# c++ compiler optimizations



## boogerlad (Jan 6, 2012)

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.


----------



## hellrazor (Jan 6, 2012)

It's initialized to gibberish because that's what the memory was when the program took it for it's own use.


----------



## boogerlad (Jan 6, 2012)

but are all variables initialized to something even before their statement?


----------



## W1zzard (Jan 6, 2012)

boogerlad said:


> but are all variables initialized to something even before their statement?



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


----------



## boogerlad (Jan 6, 2012)

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?


----------



## W1zzard (Jan 6, 2012)

boogerlad said:


> 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?



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 (Jan 6, 2012)

Would it still exist at runtime before it has been declared even if the variable was allocated to heap?


----------



## Yukikaze (Jan 6, 2012)

boogerlad said:


> Would it still exist at runtime before it has been declared even if the variable was allocated to heap?



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.


----------

