mardi 4 juillet 2017

c++ new and delete overloading

new and delete operator overloading might or might not work when compiled using different compilers and different c++ standards. Is that the normal behavior?

The following code is I used to test the compilers.

void * operator new(size_t size);
void operator delete(void * ptr) noexcept;

void * operator new(size_t size)
{
    std::cout << "1\n";
    return malloc(size);
}

void operator delete(void * ptr) noexcept
{
    std::cout << "2\n";
    free(ptr);
}

int main(void)
{
    int *n1 = new int;
    delete n1;

    int *n2 = new int[10];
    delete[] n2;
}

Here are the results I got from several compilers I have tested the code on.

mingw-w64 official build - x86_64-7.1.0-release-posix-seh-rt_v5-rev0.7z

c++11
1
2

c++14
1

clang x86_64 - v4.0.0 manually built without patches using the compiler above

c++11 and c++14
1
2

msvc - platform toolset v141 & sdk v10.0.15063.0

/std:c++14 & /std:c++latest
1
2
1
2

All tests are performed on Windows 7. I can't test compilers on GNU/Linux OS because I don't have any VMs set up.

Aucun commentaire:

Enregistrer un commentaire