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 ...
随机推荐
- form 表单模板
<div class="modal-dialog modal-lg"> //大布局modal-lg <div class="modal-content& ...
- Android开发 ---如何操作资源目录中的资源文件5 ---Raw资源管理与国际化
效果图: 1.activity_main.xml 描述: 定义两个按钮,一个是Raw资源管理,一个是处理国际化语言,其中i18n表示简体中文 <?xml version="1.0&qu ...
- 从SharePoint 2013迁移到SharePoint Online - 评估工具
博客地址:http://blog.csdn.net/FoxDave 今天想跟大家分享一款从SharePoint 2013迁移到SharePoint Online时的评估工具:SharePoint ...
- Java总结篇系列:Java泛型(转)
一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...
- poj1062(分区间迪杰斯特拉,内含测试数据,一直wa的同学可以进来看看)
昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54946 Accepted: 16518 Descripti ...
- C点滴成海------Ubuntu怎么运行C语言
Ubuntu怎么运行C语言 一.安装相关软件 安装vim:输入 sudo apt-get install vim: 安装gcc:输入 sudo apt-get install g++. 二.编写代码 ...
- LVS原理详解(3种工作方式8种调度算法)
一.集群简介 什么是集群 计算机集群简称集群是一种计算机系统,它通过一组松散集成的计算机软件和/或硬件连接起来高度紧密地协作完成计算工作.在某种意义上,他们可以被看作是一台计算机.集群系统中的单个计算 ...
- 3--Selenium环境准备--Eclipse 引入 selenium-server包
1.下载selenium-server包 selenium-server-standalone包是Seleniumd的核心jar包,其中包含了各种元素定位和调用浏览器的方法.下载jar包后,在ID ...
- 大数据-02-Scala入门
Scala 简介 它是一门基于JVM的面向函数和面向对象的编程语言, 它包含了求值表达式,闭包,切片操作,模式匹配,隐式转换等特性. 可变量/不可变量 可变集合/不可变集合.集合操作 函数 值函数 求 ...
- JAVA多线程Thread与Runnable
一.Runnable Runnable为一个之包含一个run方法的接口 public class MyRunnable implements Runnable{ @Override //表示:预示重写 ...