Ang*_*ket 8 c++ gcc stdstring gcc9
这个程序(用选项编译-std=c++17)
#include <stdio.h>
#include <string>
void* operator new(std::size_t nrOfBytes) {
printf("allocate %zd bytes on heap\n", nrOfBytes);
void* p = malloc(nrOfBytes);
if (p) {
return p;
} else {
throw std::bad_alloc{};
}
}
int main() {
// new operator is called when compiled with Clang or MSVS or GCC
int* i = new int;
delete i;
// new operator is not called when compiled with GCC
// but is called with Clang and MSVS
std::string str(2000, 'x');
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当用 Clang 或 MSVS 编译时,打印:
在堆上分配 4 个字节
在堆上分配 2016 个字节
但是,当使用 GCC(Windows 上的 MSYS 提供的 9.2.0 版)编译时,它只打印:
在堆上分配 4 个字节
我知道 GCC/libc++ 中的短字符串优化,但是对于短字符串来说 2000 个字符不是太多吗?这完全是 SSO 的问题吗?
当Windows上涉及动态库时,GCC似乎没有(或者不能?)正确地实现全局替换operator new。operator delete
请参阅错误报告,例如77726、82122和81413 。
在您的情况下,std::string构造函数和/或std::allocator<char>::allocate似乎位于标准库的动态库中,因此它operator new使用的内容没有被正确替换。