1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

compiler optimizations & code breaking

Discussion in 'Programming & Webmastering' started by Vinska, May 24, 2012.

  1. Vinska

    Vinska

    Joined:
    Jul 23, 2011
    Messages:
    1,398 (1.27/day)
    Thanks Received:
    1,219
    Location:
    Kaunas, Lithuania
    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?
    Crunching for Team TPU
  2. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    14,647 (3.93/day)
    Thanks Received:
    11,389
    what did you do that broke? that's not supposed to happen
  3. Vinska

    Vinska

    Joined:
    Jul 23, 2011
    Messages:
    1,398 (1.27/day)
    Thanks Received:
    1,219
    Location:
    Kaunas, Lithuania
    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 says thanks.
    Crunching for Team TPU
  4. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,006 (6.56/day)
    Thanks Received:
    1,955
    Location:
    Concord, NH
    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.
  5. Vinska

    Vinska

    Joined:
    Jul 23, 2011
    Messages:
    1,398 (1.27/day)
    Thanks Received:
    1,219
    Location:
    Kaunas, Lithuania
    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 =]
    Last edited: Jun 6, 2012
    Crunching for Team TPU
  6. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (5.08/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    Just out of curiosity, why are you calling printf() in a class constructor? For debugging purposes?
    Aquinus says thanks.
  7. Vinska

    Vinska

    Joined:
    Jul 23, 2011
    Messages:
    1,398 (1.27/day)
    Thanks Received:
    1,219
    Location:
    Kaunas, Lithuania
    Yeah, I could say yes. More like, for informational purposes, though.
    Crunching for Team TPU

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page