【发布时间】:2018-01-28 14:50:37
【问题描述】:
以下代码中的 4 个类:A、B、C 和 D。
他们都有一个成员operator new[]。
另外,
- B 有一个构造函数;
- C 有一个析构函数;
- D 有一个成员
operator delete[]。
输出成员operator new[]的参数size和4个类的sizeof:
new[] A 40
new[] B 40
new[] C 48
new[] D 48
sizeof(A) 4
sizeof(B) 4
sizeof(C) 4
sizeof(D) 4
size的差异是什么原因?
代码(我知道很丑):
#include <iostream>
using namespace std;
class A {
int i;
public:
static void* operator new[](std::size_t size) throw(std::bad_alloc) {
cout << "new[] A " << size << endl;
return malloc(size);
}
};
class B {
int i;
public:
static void* operator new[](std::size_t size) throw(std::bad_alloc) {
cout << "new[] B " << size << endl;
return malloc(size);
}
B() {}
};
class C {
int i;
public:
static void* operator new[](std::size_t size) throw(std::bad_alloc) {
cout << "new[] C " << size << endl;
return malloc(size);
}
~C() {}
};
class D {
int i;
public:
static void* operator new[](std::size_t size) throw(std::bad_alloc) {
cout << "new[] D " << size << endl;
return malloc(size);
}
static void operator delete[](void* p, std::size_t size) {
free(p);
}
};
int main() {
A* a = new A[10];
B* b = new B[10];
C* c = new C[10];
D* d = new D[10];
cout << "sizeof(A) " << sizeof(A) << endl;
cout << "sizeof(B) " << sizeof(B) << endl;
cout << "sizeof(C) " << sizeof(C) << endl;
cout << "sizeof(D) " << sizeof(D) << endl;
}
关于操作系统和编译器:
编译:clang++ 和 g++ 的结果相同
clang++ test.cpp -o test -std=c++11
g++ test.cpp -o test -std=c++11
操作系统:Linux Mint 18.2 Cinnamon 64 位
编译器:
clang++ -v
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.3
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.0.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.3
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.0.0
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
Found CUDA installation: /usr/local/cuda
g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.4' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
【问题讨论】:
-
由于这不是标准规定的内容,请您说明您使用的是什么平台(操作系统,32/64 位)和编译器?还有,编译时是否开启优化。
-
只有我一个吗?
prog.cc:7:51: error: ISO C++1z does not allow dynamic exception specifications使用 GCC 8。@JohnZwinck (s)他提到了 64 位。 -
不使用clang,而是使用另一个编译器。在情况 A 和 B 中,默认析构函数什么都不做,因此编译器不关心调用它。在 C 的情况下,我们有一个用户定义的构造函数被调用,因此编译器必须存储调用它的次数。可能需要一些额外的字节。与情况 D 类似,我们需要以某种方式将
size参数设置为delete[]。但只是猜测。此外,如果班级成员比int更复杂,结果可能会有所不同。 -
增加的空间是 8 个字节在 10 个对象之间共享,因此它不是每个对象的成本。
标签: c++ destructor new-operator delete-operator