8

该程序(使用选项编译-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 的问题吗?

4

1 回答 1

4

当 Windows 上涉及动态库时,GCC 似乎没有(或不能?)实现全局替换operator new并且正确。operator delete

请参阅错误报告,例如77726、8212281413

在您的情况下std::string,构造函数和/或std::allocator<char>::allocate似乎位于标准库的动态库中,因此operator new它使用的没有被正确替换。

于 2020-01-14T10:44:04.380 回答