gpt4 book ai didi

c++ - 在 C++ 中是否可以指定使用哪个删除运算符?

转载 作者:可可西里 更新时间:2023-11-01 18:39:00 25 4
gpt4 key购买 nike

我正在检测一些代码并注意到 C++14 特性有两个新的 delete 运算符(来自 http://en.cppreference.com/w/cpp/memory/new/operator_delete ):

These are 5-6) Called instead of (1-2) if a user-defined replacement is provided except that it's implementation-defined whether (1-2) or (5-6) is called when deleting objects of incomplete type and arrays of non-class and trivially-destructible class types (since C++17). The standard library implementations are identical to (1-2).

我已经重载了这些并且想专门调用这两个。当我用 gcc 重载这两个时,我没有问题。使用 clang++,我得到了对 operator delete(void*)

的 undefined reference

这是代码

void* operator new(long unsigned int howMuch) {
return reinterpret_cast<void*>(0xdeadbeef);
}

void* operator new[](long unsigned int howMuch) {
return reinterpret_cast<void*>(0xdeadbeef);
}

void operator delete(void* what, long unsigned int howmuch) {
if(what != reinterpret_cast<void*>(0xdeadbeef)) __builtin_trap();
if(howmuch != 1) __builtin_trap();
}

extern "C"
void _start() {
delete new char;
asm("syscall" : : "a"(60) : );
}

用gcc编译:g++ -ggdb -std=c++14 -nostdlib -fno-builtin -fno-exceptions 1.cc 没问题,运行正常。

是否可以用 llvm/clang 做到这一点?

最佳答案

您可以像这样显式调用大小或非大小删除运算符:

char* ptr = new char;
delete ptr; // compiler selects which to call
operator delete(ptr); // explicitly call the non-sized delete
operator delete(ptr, 1); // explicitly call sized delete

完整示例:

void* operator new(long unsigned int howMuch) {                                                                                                                                               
return reinterpret_cast<void*>(0xdeadbeef);
}

void* operator new[](long unsigned int howMuch) {
return reinterpret_cast<void*>(0xdeadbeef);
}

void operator delete(void* what) {
if(what != reinterpret_cast<void*>(0xdeadbeef)) __builtin_trap();
}

void operator delete(void* what, long unsigned int howmuch) {
if(what != reinterpret_cast<void*>(0xdeadbeef)) __builtin_trap();
if(howmuch != 1) __builtin_trap();
}

extern "C"
void _start() {
char* ptr = new char;
delete ptr;
operator delete(ptr);
operator delete(ptr, 1);
asm("syscall" : : "a"(60) : );
}

编译并查看生成的代码,很清楚在以下情况下调用了哪些运算符:

$ clang++ -std=c++14 -nostdlib  -fno-builtin  -fno-exceptions -fsized-deallocation sized-deallocation.cpp -o sized-deallocation.bin && gdb -batch -ex 'file sized-deallocation.bin' -ex 'disassemble _start' | c++filt
Dump of assembler code for function _start:
0x0000000000401070 <+0>: push %rbp
0x0000000000401071 <+1>: mov %rsp,%rbp
0x0000000000401074 <+4>: sub $0x10,%rsp
0x0000000000401078 <+8>: mov $0x1,%eax
0x000000000040107d <+13>: mov %eax,%edi
0x000000000040107f <+15>: callq 0x401000 <operator new(unsigned long)>
0x0000000000401084 <+20>: mov %rax,-0x8(%rbp)
0x0000000000401088 <+24>: mov -0x8(%rbp),%rax
0x000000000040108c <+28>: cmp $0x0,%rax
0x0000000000401090 <+32>: mov %rax,-0x10(%rbp)
0x0000000000401094 <+36>: je 0x4010aa <_start+58>
0x000000000040109a <+42>: mov $0x1,%eax
0x000000000040109f <+47>: mov %eax,%esi
0x00000000004010a1 <+49>: mov -0x10(%rbp),%rdi
0x00000000004010a5 <+53>: callq 0x401040 <operator delete(void*, unsigned long)>
0x00000000004010aa <+58>: mov -0x8(%rbp),%rdi
0x00000000004010ae <+62>: callq 0x401020 <operator delete(void*)>
0x00000000004010b3 <+67>: mov $0x1,%eax
0x00000000004010b8 <+72>: mov %eax,%esi
0x00000000004010ba <+74>: mov -0x8(%rbp),%rdi
0x00000000004010be <+78>: callq 0x401040 <operator delete(void*, unsigned long)>
0x00000000004010c3 <+83>: mov $0x3c,%eax
0x00000000004010c8 <+88>: syscall
0x00000000004010ca <+90>: add $0x10,%rsp
0x00000000004010ce <+94>: pop %rbp
0x00000000004010cf <+95>: retq
End of assembler dump.

虽然您使用 Clang 得到 undefined reference to `operator delete(void*)' 的实际原因是(如 @T.C. 所说)Clang 需要 -fsized-deallocation 标志以启用 C++14 sized deallocation .

如果使用以下命令,您的示例编译没有错误:

clang++ -ggdb -std=c++14 -nostdlib  -fno-builtin  -fno-exceptions -fsized-deallocation 1.cc

由于 Clang 3.7 C++14 大小的释放在默认情况下是禁用的:

C++ Support in Clang > C++14 implementation status > C++ Sized Deallocation N3778

(7): In Clang 3.7 and later, sized deallocation is only enabled if the user passes the -fsized-deallocation flag. The user must supply definitions of the sized deallocation functions, either by providing them explicitly or by using a C++ standard library that does. libstdc++ added these functions in version 5.0, and libc++ added them in version 3.7.

Clang 3.7 Release Notes > What’s New in Clang 3.7? > New Compiler Flags

The sized deallocation feature of C++14 is now controlled by the -fsized-deallocation flag. This feature relies on library support that isn’t yet widely deployed, so the user must supply an extra flag to get the extra functionality.

此更改的原因是当时(2015-03-19)广泛部署的标准库中缺少这些功能:

C++14: Disable sized deallocation by default due to ABI breakage

There are no widely deployed standard libraries providing sized deallocation functions, so we have to punt and ask the user if they want us to use sized deallocation. In the future, when such libraries are deployed, we can teach the driver to detect them and enable this feature.

可在此处找到此选项的手动条目:

Clang command line argument reference > Compilation flags > Target-independent compilation options

-fsized-deallocation, -fno-sized-deallocation

Enable C++14 sized global deallocation functions

关于c++ - 在 C++ 中是否可以指定使用哪个删除运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35077781/

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