从 void 指针缓冲区实例化结构体

发布于 2024-08-15 13:48:21 字数 499 浏览 3 评论 0原文

这里有一些 C++ 代码,对我来说看起来很有趣,但我知道它是有效的。

定义了一个结构体,在程序中我们使用 void 指针分配内存。然后使用分配的缓冲区创建结构。

这是一些代码

typedef struct{
 char buffer[1024];
} MyStruct

int main()
{
   MyStruct* mystruct_ptr = 0;

   void* ptr = malloc(sizeof(MyStruct));

   // This is the line that I don't understand
   mystruct_ptr = new (ptr) MyStruct();

   free(ptr);

   return 0;
}

该代码还有更多内容,但这就是要点。

我还没有测试过这段代码,但是我正在查看的代码经过了很好的测试,并且可以工作。但如何呢?

谢谢。

编辑:修复了内存泄漏。

Here's some C++ code that just looks funny to me, but I know it works.

There is a struct defined, and in the program we allocate memory using a void pointer. Then the struct is created using the allocated buffer.

Here's some code

typedef struct{
 char buffer[1024];
} MyStruct

int main()
{
   MyStruct* mystruct_ptr = 0;

   void* ptr = malloc(sizeof(MyStruct));

   // This is the line that I don't understand
   mystruct_ptr = new (ptr) MyStruct();

   free(ptr);

   return 0;
}

The code has more stuff, but that's the gist of it.

I haven't tested this code, but the code I'm looking at is very well tested, and works. But how?

Thanks.

EDIT: Fixed that memory leak.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

扛刀软妹 2024-08-22 13:48:21

这称为 placement new,它构造一个对象在预先分配的缓冲区上(您指定地址)。

编辑:更有用的链接

This is called placement new, which constructs an object on a pre-allocated buffer (you specify the address).

Edit: more useful link

野生奥特曼 2024-08-22 13:48:21

这就是新的安置。它将运行所需的任何构造函数和初始化,但您要提供内存,而不是让 new 为您分配内存。

本网站已提供详细信息。

That is the placement new. It will run any constructors and initialization needed, but you are supplying the memory instead of having new allocate it for you.

Details have already been provided on this site.

我家小可爱 2024-08-22 13:48:21

这是新的安置。这告诉 new 返回一个特定的地址而不是实际分配内存。但重要的是,它仍在调用构造函数。

当您需要在特定内存地址创建对象时,需要使用此技术。

This is placement-new. This tell new to return a specific address instead of actually allocating memory. But importantly, it is still invoking the constructor.

This technique is needed when you need to create an object at a specific memory address.

他夏了夏天 2024-08-22 13:48:21

Scott Meyers 在 高效的 C++

Scott Meyers describes this technique very well in Effective C++.

红颜悴 2024-08-22 13:48:21

该构造是新放置的。编译器不是分配内存并调用类构造函数,而是在指定的内存位置构造实例。这种对内存分配和释放的控制对于优化长时间运行的程序非常有用。

That construct is placement new. Rather than allocating memory and invoking the class constructor the compiler constructs the instance in the memory location specified. This sort of control over memory allocation and deallocation is tremendously useful in optimizing long running programs.

江城子 2024-08-22 13:48:21

在 Google 上搜索“新展示位置”。

Search Google for "placement new".

兲鉂ぱ嘚淚 2024-08-22 13:48:21

如果您将一个文件读取放在 malloc 之后但 new 之前,那么您将执行常见的(但丑陋的) Load-In-Place hack,用于在序列化缓冲区中创建预初始化的 C++ 对象。

If you put a file read after the malloc but before the new, you'd be doing the common (but ugly) Load-In-Place hack for creating pre-initialized C++ objects in a serialized buffer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文