8

This program (compiled with option -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;
}

when compiled with Clang or MSVS, prints:

allocate 4 bytes on heap

allocate 2016 bytes on heap

However, when compiled with GCC (Version 9.2.0 provided by MSYS on Windows) it only prints:

allocate 4 bytes on heap

I am aware of short string optimization in GCC/libc++, but aren't 2000 chars too many for a short string? Is it a matter of SSO at all?

1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Jan 14, 2020 at 10:47

1 Answer 1

4

It seems that GCC does not (or can't?) implement replacement of the global operator new and operator delete correctly when dynamic libraries are involved on Windows.

See bug reports e.g. 77726, 82122 and 81413.

In your case std::string's constructor and/or std::allocator<char>::allocate seem to be located in the standard library's dynamic library, so that the operator new it uses isn't being replaced correctly.

Sign up to request clarification or add additional context in comments.

6 Comments

To further clarify: this behavior has only been observed when using GCC with libc++ on Windows provided by MSYS / MINGW. Every version of GCC/libstdc++ on Ubuntu I was able to test worked exactly like those programs compiled with Clang or MSVS. Moreover, it is clearly not a case of SSO when compiled with GCC, as sizeof(str) yields 32.
@Angle.Bracket Do you mean libstdc++ instead of libc++?
I guess its called libc++ in MINGW
@Angle.Bracket libc++ usually refers to the standard library implementation of the LLVM project for clang. I don't think gcc is able to use it, mingw or not. But I may be wrong here.
@Angle.Bracket I guess you can verify whether you are using libstdc++ or libc++ using the answers in this question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.