Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22256

What's the right behavior when overloading operator delete without overloading new? gcc and clang differ

$
0
0

The following code behaves differently with clang (version 5.0.0-3~16.04.1 on x86_64-pc-linux-gnu) and gcc (9.2.0).

void operator delete(void* ptr) noexcept {
    std::cout << "overloaded delete"<< std::endl;
    free(ptr);
}

int main() {
    int* pint = new int;
    delete pint; // clang doesn't go through overloaded operator delete, gcc does
}

gcc goes via the overloaded operator delete while clang avoids it preferring the global delete.

(When adding an overloaded operator new both compilers agree and go through both overloaded versions).

Code: http://coliru.stacked-crooked.com/a/0903bd53a8c04a9b


Viewing all articles
Browse latest Browse all 22256

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>