gpt4 book ai didi

c++ - 自定义内存分配器 : T* pointer, operator new 与 void pointer cast

转载 作者:行者123 更新时间:2023-11-30 01:53:24 25 4
gpt4 key购买 nike

我根据 this gamedev.net post 中的代码创建了一些自定义内存分配器.

文章中描述的实用程序模板之一是这样声明的:

template <class T> T* allocateNew(Allocator& allocator, T& t) {
return new (allocator.allocate(sizeof(T), alignof(T))) T(t);
}

* 我将 __alignof() 转换为对 alignof() 的调用,因为我使用的是 C++11

我认为这段代码为新的 T 对象分配内存,从引用复制 t 堆栈对象到新分配的堆内存并返回 T* 指向新对象的指针。

根据这个假设,我将上面的代码转换为:

template <class T> T* allocateNew(Allocator& allocator, T& t) {
void *ptr = allocator.allocate (sizeof (T), alignof (T));
assert(ptr && "So that I don't dereference a null pointer");
* (T *) (ptr) = instance; // Casting void* to T* and then dereferencing
return (T *) ptr;
}

它似乎工作得很好。我的问题是:

  • new 与 void 指针转换有什么不同吗?
  • 两者在性能上有什么区别吗?在 G++ 中,我找不到任何显着的速度差异
  • 第二个代码示例是否存在任何漏洞(除了我通过断言检查的潜在空指针取消引用之外)?

问候,TM3P

最佳答案

Does new do anything different than the void pointer casting does?

是的;它构造一个新对象,而不是尝试分配给一个实际不存在的对象。

Is there any performance difference between the two?

对于普通类型,初始化和赋值实际上是同一件事,因此可能差别很小或没有差别。对于非平凡类型,它们调用不同的用户定义函数(构造函数与赋值运算符),这可能会做非常不同的事情。

Does the second code sample have any vulnerabilities?

对于非平凡的类型,赋值运算符可能会在赋值之前对对象的状态做出假设,并且如果没有有效的对象,则可能会出现可怕的错误,使程序陷入未定义的行为。

tl;dr 放置新作品,狡猾的类型转换是不好的。

关于c++ - 自定义内存分配器 : T* pointer, operator new 与 void pointer cast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23171191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com