Skip to content

Instantly share code, notes, and snippets.

@icebreaker
Created November 15, 2016 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icebreaker/14ca27556be954124ea04d4e34eff043 to your computer and use it in GitHub Desktop.
Save icebreaker/14ca27556be954124ea04d4e34eff043 to your computer and use it in GitHub Desktop.
weird clang++ / g++ behavior
$ clang++ -O0 test1.cpp test2.cpp -o simple; ./simple
destructor 1
destructor 1
$ clang++ -O1 test1.cpp test2.cpp -o simple; ./simple
destructor 1
destructor 1
$ clang++ -O2 test1.cpp test2.cpp -o simple; ./simple
destructor 1
destructor 2
$ clang++ -O3 test1.cpp test2.cpp -o simple; ./simple
destructor 1
destructor 2
$ g++ -O0 test1.cpp test2.cpp -o simple; ./simple
destructor 1
destructor 1
$ g++ -O1 test1.cpp test2.cpp -o simple; ./simple
destructor 1
destructor 2
$ g++ -O2 test1.cpp test2.cpp -o simple; ./simple
destructor 1
destructor 2
$ g++ -O3 test1.cpp test2.cpp -o simple; ./simple
destructor 1
destructor 2
#include <stdio.h>
#include "test2.hpp"
struct data
{
~data()
{
printf("destructor 1\n");
}
};
int main(int argc, char *argv[])
{
data *a = new data;
delete a;
test_data();
}
#include <stdio.h>
struct data
{
~data()
{
printf("destructor 2\n");
}
};
void test_data(void)
{
data *a = new data;
delete a;
}
void test_data(void);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment