该程序(使用选项编译-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;
}
当使用 Clang 或 MSVS 编译时,打印:
在堆上分配 4 个字节
在堆上分配 2016 个字节
但是,当使用 GCC(Windows 上 MSYS 提供的版本 9.2.0)编译时,它只打印:
在堆上分配 4 个字节
我知道 GCC/libc++ 中的短字符串优化,但是对于短字符串来说,2000 个字符不是太多了吗?这完全是 SSO 的问题吗?