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. 深入理解java虚拟机---java虚拟机的发展史(四)

    1.java虚拟机 A:java虚拟机有很多个版本,但是我们经常使用的是sun公司的HotSpot,可以通过以下命令获取java虚拟机版本 B:JAVA虚拟机分类: 1.Sun Class VM 2. ...

  2. [Paper] LCS: An Efficient Data Eviction Strategy for Spark

    Abstract Classical strategies do not aware of recovery cost, which could cause system performance de ...

  3. 查看oracle数据库允许的最大连接数和当前连接数

    1.查看当前的数据库连接数  select count(*) from v$process ;    --当前的数据库连接数 2.数据库允许的最大连接数  select value from v$pa ...

  4. php优秀框架codeigniter学习系列——common.php

    文件位于system/core/common.php,是框架核心文件. 该文件中定义了一系列的函数,都是框架运行中经常需要用到的.下面逐一介绍. is_php /** * Determines if ...

  5. python-tornado和django优缺点

    Django优点: 大和全(重量级框架)自带orm,template,view 需要的功能也可以去找第三方的app注重高效开发全自动化的管理后台(只需要使用起ORM,做简单的定义,就能自动生成数据库结 ...

  6. mysql三范式

    第一范式:有主键,具有原子性,字段不可分割. 第二范式:完全依赖,没有部分依赖. 第三范式:没有传递依赖. 总结:数据库设计尽量遵循三范式,但是还是根据实际情况进行取舍,有时候会拿冗余还速度,最总用的 ...

  7. Spring REST API

    RPC是面向服务的,并关注与行为和动作:而REST是面向资源的,强调描述应用程序的事务的名词.REST将资源的状态以最适合客户端或服务端的形式从服务器端转移到客户端. REST Representat ...

  8. [转]Skynet之斗转星移 - 将控制权交给Lua

      Skynet之斗转星移 - 将控制权交给Lua http://www.outsky.org/code/skynet-lua.html Sep 7, 2014 在我看来,Skynet的一个重要优势是 ...

  9. Gauss error function

    0. error function erf(x)=1π∫−xxe−t2dt" role="presentation">erf(x)=1π−−√∫x−xe−t2dte ...

  10. 关闭或开启memory_target

    对于Oracle数据库内存参数配置大小,一般满足如下规则 sga_target + pga_aggregate_target < memory_target <=memory_max_ta ...