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 ...
随机推荐
- 查看json数据更新情况
#! python3 # -*- coding:utf8 -*- #主要为读取excel中接口地址,打开网页爬取url页面中数据,解析json,检查是否符合逻辑(正常) import requests ...
- Cracking The Coding Interview 4.0_二叉树
#include <iostream> #include <string> using namespace std; class tree { public: tree() { ...
- 谷歌开源的TensorFlow Object Detection API视频物体识别系统实现(二)[超详细教程] ubuntu16.04版本
本节对应谷歌开源Tensorflow Object Detection API物体识别系统 Quick Start步骤(一): Quick Start: Jupyter notebook for of ...
- AVD Manager 模拟器使用
一.模拟器配置 1.双击启动AVD Manager,进入配置界面 2.点Create按钮创建 3.配置模拟器基本信息 --AVD Name:设备名称,自己定义一个,用英文(不要用中文) --Devic ...
- Nginx web服务器
文件读取会使用到以下几个配置 1. sendfile 使用nginx作为静态资源服务时,通过配置sendfile可以有效提高文件读取效率,设置为on表示启动高效传输文件的模式.sendfile可以让N ...
- Day3作业及默写
1.有变量量name = "aleX leNb" 完成如下操作: 移除 name 变量对应的值两边的空格,并输出处理结果 print(name.strip()) 移除 name 变 ...
- 去掉 input type="number" 在浏览器中遗留的图标样式
input::-webkit-outer-spin-button,input::-webkit-inner-spin-button{ /* chrome */ -webkit-appeara ...
- 2017ICPC南宁赛区网络赛 Train Seats Reservation (简单思维)
You are given a list of train stations, say from the station 111 to the station 100100100. The passe ...
- HDU 6060 17多校3 RXD and dividing(树+dfs)
Problem Description RXD has a tree T, with the size of n. Each edge has a cost.Define f(S) as the th ...
- 终极C语言指针
// ex1.cpp : Defines the entry point for the console application. // #include "stdafx.h" # ...