【发布时间】:2014-08-23 14:17:43
【问题描述】:
我对子对象的动态内存分配过程有疑问。 代码:
#include <iostream>
using std::cout;
using std::endl;
struct S
{
long l;
};
struct V
{
long d;
};
struct A
{
int a;
};
struct B : A, S, V
{
void * operator new(std::size_t t)
{
cout << t << endl;
return ::operator new(t);
}
};
B *b = new B; // Prints 12 for g++ and 24 for clang++.
int main()
{
b -> a = 23;
b -> l = 3L;
b -> d = 52L;
}
g++ 和clang++ 的结果不同。但是因为8 + 8 + 4 = 20,我预计会打印20。您能解释一下传递给运算符 new 的参数值的意义吗?
【问题讨论】: