Those optimizers can be sneaky. I don't know how long you've been doing this, but I remember when the first C and C++ optimizing compilers for PC came out (late '80s, roughly) that they did a lot of nasty things and you had to be very careful with what optimizations you picked. Let's just say it was a rocky start.
I was on a project with some C++ just three years ago and it's a good thing my supervisor was borderline genius because he caught VC++ doing some really strange things. The ways he figured it out was beyond the skills of pretty much everyone else I know.
I've been using C/C++ since around 1990 or so. The old problems were usually weird compiler bugs when optimizations went wrong dramatically, from what I can recall. But I was thinking more of cases like this, which tripped me up recently - I'll simplify, of course:
int*p=nullptr;// Start with a NULL pointer.int&r=*p;// Later on, convert to a reference, probably in a function call.int*i=&r;// Convert the reference back to a pointer.if(i!=nullptr){// If it's not NULLstd::cout<<"i is now "<<*i<<std::endl;// Print it.}
That code crashes with a segfault trying to deference a NULL pointer. This is because the standard says that if the address of a reference is the NULL pointer, it's Undefined Behaviour. So most modern compilers now choose to optimize away the test, even without optimization turned on...
Those optimizers can be sneaky. I don't know how long you've been doing this, but I remember when the first C and C++ optimizing compilers for PC came out (late '80s, roughly) that they did a lot of nasty things and you had to be very careful with what optimizations you picked. Let's just say it was a rocky start.
I was on a project with some C++ just three years ago and it's a good thing my supervisor was borderline genius because he caught VC++ doing some really strange things. The ways he figured it out was beyond the skills of pretty much everyone else I know.
I've been using C/C++ since around 1990 or so. The old problems were usually weird compiler bugs when optimizations went wrong dramatically, from what I can recall. But I was thinking more of cases like this, which tripped me up recently - I'll simplify, of course:
That code crashes with a segfault trying to deference a NULL pointer. This is because the standard says that if the address of a reference is the NULL pointer, it's Undefined Behaviour. So most modern compilers now choose to optimize away the test, even without optimization turned on...
"Undefined behaviour" is hell.