【发布时间】:2016-09-10 22:57:32
【问题描述】:
我找到了这个sn-p
void* operator new(size_t nbytes)
{
if (nbytes == 0)
nbytes = 1; // so all alloc's get a distinct address
void* ans = malloc(nbytes + 4); // overallocate by 4 bytes
*(Pool**)ans = NULL; // use NULL in the global new
return (char*)ans + 4; // don't let users see the Pool*
}
这里https://isocpp.org/wiki/faq/dtors
我现在花了一个多小时试图理解*(Pool**)ans = NULL; 的作用。
ans 是一个 void 指针,所以我假设它被强制转换为 Pool 指针并且池设置为 0。不是指针而是池本身,因为左侧的第三个 *。但是 Pool 没有定义 operator=。
声明中的pointer** 显然是指向指针的指针...但在这种情况下,这对我来说毫无意义,因为ans 是单个指针。
【问题讨论】: