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. [Linux]Linux下修改snmp协议的默认161端口

    一.Linux SNMP的配置 SNMP的简介和Linux下IPV4,IPV6地址的snmp协议开启可以参考上一个随笔:[Linux]CentOS6.9开启snmp支持IPV4和IPV6 二.修改默认 ...

  2. 第三篇 功能实现(3) (Android学习笔记)

    第三篇 功能实现(3) ●发一个广播和启动一个隐式的Intent非常像,那么它们之间有什么区别呢? Implicit Intents (sent via startActivity( )) and B ...

  3. [leetcode53]最长子数组 Maximum Subarray Kadane's算法

    [题目] Given an integer array nums, find the contiguous subarray (containing at least one number) whic ...

  4. Docker(2):快速入门及常用命令

    什么是Docker? Docker 是世界领先的软件容器平台.开发人员利用 Docker 可以消除协作编码时“在我的机器上可正常工作”的问题.运维人员利用 Docker 可以在隔离容器中并行运行和管理 ...

  5. 写的一个ORACLE存储过程小练习

    CREATE OR REPLACE PROCEDURE PRO_1112(O_NOTE OUT NUMBER,O_RESULT OUT VARCHAR2)ASV_NO NUMBER(20);V_NOT ...

  6. Appium Python API

    1.contexts contexts(self): Returns the contexts within the current session. 返回当前会话中的上下文,使用后可以识别H5页面的 ...

  7. mysql 数据库关于my.int 的相关问题

    最好在建库的时候直接建好 create database db1 charset utf8; my.int  在mysql的目录里 名曰配置文件    里面主要是内容就是 1 一般用到的就是编码不统一 ...

  8. wiki----为用户设置管理员权限

    wiki页面的管理员权限设置方法: wiki的页面好像没办法修改,只能在数据库中进行操作: 1.进入到wiki的安装目录下: #cd /var/www/html/wiki 2.查看wiki的配置文件, ...

  9. Pandas分组统计函数:groupby、pivot_table及crosstab

    利用python的pandas库进行数据分组分析十分便捷,其中应用最多的方法包括:groupby.pivot_table及crosstab,以下分别进行介绍. 0.样例数据 df = DataFram ...

  10. eclipse 设置Java快捷键补全

    打开Eclipse,点击Window--Preferences--Java--Editor--ContentAssist Auto Activation 勾选Enable auto activatio ...