在特定内存地址创建新的C ++对象? [英] Create new C++ object at specific memory address?

查看:94
本文介绍了在特定内存地址创建新的C ++对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++中是否可以在特定的内存位置创建一个新对象?我有一个共享内存块,我想在其中创建一个对象。

Is it possible in C++ to create a new object at a specific memory location? I have a block of shared memory in which I would like to create an object. Is this possible?

推荐答案

您要刊登位置 new ()。它基本上使用现有内存块调用构造函数,而不是从堆中分配新的内存。

You want placement new(). It basically calls the constructor using a block of existing memory instead of allocating new memory from the heap.

编辑:确保您了解注意,在使用之前,负责为使用 new()创建的对象显式调用析构函数!

make sure that you understand the note about being responsible for calling the destructor explicitly for objects created using placement new() before you use it!

这篇关于在特定内存地址创建新的C ++对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆