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的更多相关文章

  1. Embedded System.

    Soc ( System on Chip) Soc is an integrated circuit (IC) that integrates all components of a computer ...

  2. 嵌入式(Embedded System)笔记 —— Cortex-M3 Introduction and Basics(上)

    随着课内的学习,我想把每节课所学记录下来,以作查阅.以饲读者.由于我所上的是英文班课程,因此我将把关键术语的英文给出,甚至有些内容直接使用英文. 本次所介绍内容是关于Cortex-M3的基础内容. - ...

  3. 微软职位内部推荐-SW Engineer II for Embedded System

    微软近期Open的职位: Do you have a passion for embedded devices and services? &nbsp Does the following m ...

  4. 嵌入式(Embedded System)笔记 —— Cortex-M3 Introduction and Basics(下)

    随着课内的学习,我想把每节课所学记录下来,以作查阅.以饲读者.由于我所上的是英文班课程,因此我将把关键术语的英文给出,甚至有些内容直接使用英文. 本次所介绍内容仍是关于Cortex-M3的基础内容,相 ...

  5. The key of real time embedded system

    对于实时嵌入式系统来说,最重要的是每一个进程所需时间的可检测性,可预测性.要不你的实时性是没有办法保证的.有些时候你对一些没有从事过嵌入式开发的人谈这个进程(TASK)设计是按8ms被调度一次,他们会 ...

  6. Single-stack real-time operating system for embedded systems

    A real time operating system (RTOS) for embedded controllers having limited memory includes a contin ...

  7. Embedded之Introduction

    1 Embedded system An embedded system is a combination of computer hardware and software, and perhaps ...

  8. Using QEMU for Embedded Systems Development

    http://www.opensourceforu.com/2011/06/qemu-for-embedded-systems-development-part-1/ http://www.opens ...

  9. MPU/SoC/Application Processor/Embedded OS

    Everything has its principles and mechanisms which are designed by its creator and followed by its u ...

随机推荐

  1. 四川省赛 SCU - 4438

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...

  2. Day20作业及默写

    1.请使用C3算法计算出链接图中的继承顺序-Link 一 graph BT id1[A]-->id2[B] id2[B]-->id6[F] id6[F]-->id7[G] id1[A ...

  3. Vue 之 Vue.nextTick()

    异步更新队列 可能你还没有注意到,Vue 异步执行 DOM 更新.只要观察到数据变化,Vue 将开启一个队列,并缓冲在同一事件循环中发生的所有数据改变.如果同一个 watcher 被多次触发,只会一次 ...

  4. JAVA小程序-----买衣服

    import java.util.Scanner; //引用扫描器 public class TestDemo1 { public static void main(String [] args){ ...

  5. 第76课 最小生成树(Prim)

    图解: 添加prim函数: #ifndef GRAPH_H #define GRAPH_H #include "Object.h" #include "SharedPoi ...

  6. 正则表达式 —— Cases 与 Tricks

    1. cases 匹配任意单词(两侧可以有多个空格): ( +[a-zA-Z]+ +) 上述表达式无法匹配句子末尾的单词,若想匹配句尾或者逗号前的单词,则可拓展为: ( +[a-zA-Z]+[?,.] ...

  7. Ubuntu add sudo

    为了安全起见,ubuntu中的普通用户一般没有root权限,因此即使知道管理员密码也无法使用sudo,但这个情况可以通过加入sudoer列表或者加入sudo组来改变. 拓展: 不管使用哪种方式,使得一 ...

  8. 【转载】 从ACM会议看中国大陆计算机科学与国外的差距

    ps:   这是一篇06年的文章,与今日的国内计算机行业学术圈环境简直是天翻地覆,很不错的history,值得mark下,今日的cs学术发展十号是坏不发表意见,但是history是值得对比,借鉴,思考 ...

  9. tarfile — Read and write tar archive files

    参考: https://docs.python.org/2/library/tarfile.html http://www.jianshu.com/p/bbad16822eab #解压文件tarfil ...

  10. POJ - 1474 :Video Surveillance (半平面交-求核)

    pro:顺时针给定多边形,问是否可以放一个监控,可以监控到所有地方,即问是否存在多边形的核. 此题如果两点在同一边界上(且没有被隔段),也可以相互看到. sol:求多边形是否有核.先给直线按角度排序, ...