# compiler optimizations & code breaking



## librin.so.1 (May 24, 2012)

Greetings fellow programmers! xD

Ok, ./serious_mode

Recently I came across a strange thing: code I wrote was working fine, but as soon as I decided to make a non-debug build (has compiler optimizations on), I noticed a strange thing - my program promptly segfaulted each time I ran it. If I rebuild it with compiler optimizations off, it then works fine again. That was a little shocking to me.
I then searched the web (which was mostly confined to reading stuff on stackoverflow.com). I found out that others have also encountered this. Some with long time programming experience encountered this many times and sometimes even expect this to happen sooner or later with growing projects. Though, pretty much every instance I found people mentioning this, were just small sidenotes to some discussions involving compiler optimizations, thus, not discussed any more deeper than that.

So I would like to ask You all to share Your thoughts on such a thing - "how, why, when, etc."
And I mean, in general sense. Not just about this specific case I encountered.

One of my thoughts - compiler optimizations, AFAIK, according to standard (at least on C & C++) state that it should not alter the a program works in a way that would produce different results compared to a non-optimized one. That it normally does - makes sure it runs the same. If the program does run and behave differently (like my previously mentioned segfault), I think it is not automatically a compiler's fault. If one were to write his/her code in such a way that confuses the compiler, it could accidentally make the program behave in an unintended way.
Thus, I wonder, if a person produces code that is handled by the compiler this way, could it be an indication that the said person's coding style is not good and/or that this person is writing his/her code in such a way that would make any experienced hacker frown?


----------



## W1zzard (May 24, 2012)

what did you do that broke? that's not supposed to happen


----------



## librin.so.1 (May 24, 2012)

W1zzard said:


> what did you do that broke? that's not supposed to happen



I am not sure, did not have time (and enough will) to debug it yet.
That reminds me - I already spent hours on TPU, while I should be studying for tomorrow ~___~


----------



## Aquinus (May 24, 2012)

Vinska said:


> I am not sure, did not have time (and enough will) to debug it yet.
> That reminds me - I already spent hours on TPU, while I should be studying for tomorrow ~___~



I have a bad habit of doing this too. If you can find the offending line(s) of code, we can try to help you out.


----------



## librin.so.1 (Jun 6, 2012)

LOL!

(the code is in C++, btw)

EDIT: In a constructor of one class, printf() is called. It appears that when compiled with optimizations, after calling prinf(), one register that is holding the address of that object, gets overwritten to 0x1
Then a couple tens of lines and some calls later, it get dereferenced... *segfault*

EDIT2: Fix'd this with a little pinch of inline-asm =]


----------



## Kreij (Jun 6, 2012)

Just out of curiosity, why are you calling printf() in a class constructor? For debugging purposes?


----------



## librin.so.1 (Jun 6, 2012)

Kreij said:


> Just out of curiosity, why are you calling printf() in a class constructor? For debugging purposes?



Yeah, I could say yes. More like, for informational purposes, though.


----------

