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

c++ compiler optimizations

Discussion in 'Programming & Webmastering' started by boogerlad, Jan 6, 2012.

  1. boogerlad

    Joined:
    Jun 29, 2006
    Messages:
    227 (0.06/day)
    Thanks Received:
    12
    Hi all, I've been debugging my very basic test program, using eclipse and gcc 4.6.2. I noticed that when I put breakpoints on all lines of my code, and then debug, even on the first line printing "Hello World!", my variable xt is already initialized to some bizarre value. And so is my derp variable. All compiler optimizations are off. Oh and btw does anyone know what the (x) symbol means? I circled it. [​IMG]
     
    10 Year Member at TPU
  2. hellrazor

    hellrazor

    Joined:
    Feb 18, 2010
    Messages:
    1,675 (0.67/day)
    Thanks Received:
    341
    It's initialized to gibberish because that's what the memory was when the program took it for it's own use.
     
    boogerlad says thanks.
  3. boogerlad

    Joined:
    Jun 29, 2006
    Messages:
    227 (0.06/day)
    Thanks Received:
    12
    but are all variables initialized to something even before their statement?
     
    10 Year Member at TPU
  4. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    16,544 (3.60/day)
    Thanks Received:
    15,658
    ALWAYS initialize variables. the system does not guarantee any value for it. could be 0 in your testing and end up as something else on your customer's system

    your variables end up in some random piece of memory where they end up with the value that was previously in that memory cell
     
    10 Year Member at TPU
  5. boogerlad

    Joined:
    Jun 29, 2006
    Messages:
    227 (0.06/day)
    Thanks Received:
    12
    but the breakpoint is still at the helloworld line! Why are there even variables that exists? Don't they only come into existence on the line they are declared, ie line 2 and 6?
     
    10 Year Member at TPU
  6. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    16,544 (3.60/day)
    Thanks Received:
    15,658
    the variable always exists (in the same function), in a way that the program knows where its memory location is. you obviously can't use it before you declare it but that's a language construct
     
    boogerlad says thanks.
    10 Year Member at TPU
  7. boogerlad

    Joined:
    Jun 29, 2006
    Messages:
    227 (0.06/day)
    Thanks Received:
    12
    Would it still exist at runtime before it has been declared even if the variable was allocated to heap?
     
    10 Year Member at TPU
  8. Yukikaze

    Yukikaze

    Joined:
    Sep 24, 2008
    Messages:
    2,376 (0.79/day)
    Thanks Received:
    530
    Location:
    Hillsboro, Oregon, USA
    Every variable "exists" when you are in the scope of that variable. For things allocated via new/malloc the pointer itself exists as soon as you enter its scope (and may contain some arbitrary value and thus point to an arbitrary location in memory), the actual memory location for the variable you allocate via new/malloc is allocated upon the execution of the new/malloc call. These calls pick a free memory location and assign it for your use. They do no guarantee the contents of the allocated location.
     
    boogerlad says thanks.

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