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音频驱动学习之:(1)ASOC分析

    一.音频架构概述 (1)ALSA是Advanced Linux Sound Architecture 的缩写,目前已经成为了linux的主流音频体系结构,想了解更多的关于ALSA的这一开源项目的信息和 ...

  2. 深入理解java虚拟机---虚拟机工具VisualVM(十九)

    性能分析神器VisualVM 9602 VisualVM 是一款免费的,集成了多个 JDK 命令行工具的可视化工具,它能为您提供强大的分析能力,对 Java 应用程序做性能分析和调优.这些功能包括生成 ...

  3. TagHelpers 使用

    @using AuthoringTagHelpers @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers // 手动高亮

  4. win10 安装 mysql-8.0.12

    安装mysql 8 1.下载 https://dev.mysql.com/downloads/mysql/ 2.设置环境变量 将你解压后的文件里边的bin目录加入到path中.例如:D:\develo ...

  5. mybatis学习(五)----实现关联表查询

    一.一对一的表查询 查询班级表中班级号为1的对应的记录(包括教师的具体信息) 1.首先建立数据表 数据表class和techear,class表中只有一个外键techear_id,sql脚本如下: C ...

  6. SharePoint Framework 配置你的SharePoint客户端web部件开发环境

    博客地址:http://blog.csdn.net/FoxDave 你可以使用Visual Studio或者是你自己的开发环境来构建SharePoint客户端web部件.你可以使用Mac.PC或是 ...

  7. java学习笔记21(迭代器)

    java中有很多集合,内部有各种的存储的方法,取出的方法也各不相同,那么有没有一种通用的方法来取出来呢? java提供的遍历集合元素的方法有两种: 1.for-each结构(增强型for循环) 格式: ...

  8. vue安装流程

      一.环境搭建 vue推荐开发环境: Node.js: javascript运行环境(runtime),不同系统直接运行各种编程语言 npm: Nodejs下的包管理器.由于国内使用npm会很慢,这 ...

  9. SQL注入之Sqli-labs系列第二十一关(基于复杂性的cookie POST报错注入)和二十二关(基于双引号的cookie POST报错注入)

    开始挑战第二十一关(Cookie Injection- Error Based- complex - string) 和二十二关(Cookie Injection- Error Based- Doub ...

  10. maven 细节 —— scope、坐标

    对于 idea 开发环境,测试代码便是在 src/test/java(该java目录会在创建时标注为测试文件夹) 目录下的 .java 代码为测试代码: 1. scope scope的分类 compi ...