Using C++ new() placement in embedded system
For new(), there are three definition in C++11, which are listed below.
throwing (1) |
void* operator new (std::size_t size); |
nothrow (2) |
void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept; |
placement (3) |
void* operator new (std::size_t size, void* ptr) noexcept; |
The item (1) will return a non-null pointer to this block and throw a bad_alloc exception if the allocation is failure. The item (2) is same as item (1) but return a nullptr without exception. See example below.
struct TestClass {
int data[];
TestClass() {std::cout << "constructed [" << this << "]\n";}
}; void MemoryTest(void)
{
std::cout << "1: "; TestClass * p1 = new TestClass; //throw std::cout << "2: ";
TestClass * p2 = new (std::nothrow) TestClass; //no throw std::cout << "3: ";
TestClass * p3 = new (p2) TestClass; //replacement delete p1;
delete p2;
}
The output is below.
1: constructed [0x3a9e00]
2: constructed [0x3a2768]
3: constructed [0x3a2768]
We can see the placement new() did not allocate memory. It can be used in embedded system for register operation. Example is shown below. It does not allocate the memory, you can use pControlReg->IsReady(), etc.freely;
class ControlRegC {
public:
bool IsReady() const;
bool InterruptsEnabled() const;
void EnableInterrupts();
void DisableInterrupts();
private:
volatile uint8_t regValue;
}; void NewControlReg(void)
{
ControlRegC * const pControlReg1 = new (reinterpret_cast<void*>(0xFFFF0010)) ControlRegC; //0xFFFF0010 is the register address.
ControlRegC * const pControlReg2 = reinterpret_cast<ControlRegC*>(0xFFFF0010); //here is another solution, using reinterpret_cast directly
}
Using C++ new() placement in embedded system的更多相关文章
- Embedded System.
Soc ( System on Chip) Soc is an integrated circuit (IC) that integrates all components of a computer ...
- 嵌入式(Embedded System)笔记 —— Cortex-M3 Introduction and Basics(上)
随着课内的学习,我想把每节课所学记录下来,以作查阅.以饲读者.由于我所上的是英文班课程,因此我将把关键术语的英文给出,甚至有些内容直接使用英文. 本次所介绍内容是关于Cortex-M3的基础内容. - ...
- 微软职位内部推荐-SW Engineer II for Embedded System
微软近期Open的职位: Do you have a passion for embedded devices and services?   Does the following m ...
- 嵌入式(Embedded System)笔记 —— Cortex-M3 Introduction and Basics(下)
随着课内的学习,我想把每节课所学记录下来,以作查阅.以饲读者.由于我所上的是英文班课程,因此我将把关键术语的英文给出,甚至有些内容直接使用英文. 本次所介绍内容仍是关于Cortex-M3的基础内容,相 ...
- The key of real time embedded system
对于实时嵌入式系统来说,最重要的是每一个进程所需时间的可检测性,可预测性.要不你的实时性是没有办法保证的.有些时候你对一些没有从事过嵌入式开发的人谈这个进程(TASK)设计是按8ms被调度一次,他们会 ...
- Single-stack real-time operating system for embedded systems
A real time operating system (RTOS) for embedded controllers having limited memory includes a contin ...
- Embedded之Introduction
1 Embedded system An embedded system is a combination of computer hardware and software, and perhaps ...
- Using QEMU for Embedded Systems Development
http://www.opensourceforu.com/2011/06/qemu-for-embedded-systems-development-part-1/ http://www.opens ...
- MPU/SoC/Application Processor/Embedded OS
Everything has its principles and mechanisms which are designed by its creator and followed by its u ...
随机推荐
- Turing equation
Turing equation 时间限制: 1 Sec 内存限制: 128 MB 题目描述 The fight goes on, whether to store numbers starting w ...
- VS2010编译Unigine_2010源码
VS2010编译Unigine_2010源码[Debug版本] 1.Laucher工程属性改为控制台项目 2.Unigine工程编译时的Warnning LNK2019 a.属性--常规-目标文件名改 ...
- Java作业五
1.编程生成10个1~100之间的随机数,并统计每个数出现的概率. 这个博文里面又random的详细解释:https://www.cnblogs.com/ningvsban/p/3590722.htm ...
- 201621123001 《Java程序设计》第7周学习总结
1. 本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 掌握了NetBeans基本使用方法 掌握布局管理器基本概念 尝试了自己在界面布置组件 网格组布 ...
- django的FBV和CBV的装饰器例子
备忘 def auth(func): def inner(request,*args,**kwargs): u = request.COOKIES.get('username111') if not ...
- Ubuntu 修改 /etc/resolv.conf 被清空 或重启不生效解决
sudo gedit /etc/NetworkManager/NetworkManager.conf 注释掉 dns=dnsmasq [main] plugins=ifupdown,keyfile,o ...
- kettle在linux下执行任务
1.下载 最新版下载 7.1 https://community.hitachivantara.com/docs/DOC-1009855 准备 上传任务文件 .kjb,.ktr 上传mysql 驱动 ...
- 基于Scrapy-Redis和docker技术在单机上构建分布式爬虫
准备工作: 安装docker https://www.docker.com/docker-ubuntu 选择ce版本 通过docker pull 下载基础镜像,ubuntu16.04, redis, ...
- MySQL输入密码后闪退
刚刚我遇到这个问题,服务里MySQL是启状态的,所以我求助百度,发现很多种说法,我试了几个,还是不行,后来想起来我的密码不对,于是换了正确的密码试了一下,没毛病,进去了. 所以输入密码闪退时,首先确定 ...
- global $GLOBALS 区别
PHP代码 复制代码 代码如下: <?php // 例子1 function test_global() { global $var1, $var2; $var2 =& $var1; } ...