• We've upgraded our forums. Please post any issues/requests in this thread.

c++ compiler optimizations

Joined
Jun 29, 2006
Messages
227 (0.05/day)
Likes
12
#1
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.
 
Joined
Feb 18, 2010
Messages
1,715 (0.60/day)
Likes
408
System Name Eldritch
Processor AMD Ryzen 5 1500X
Motherboard MSI B350 PC Mate
Cooling Satan's butthole after going to Taco Bell
Memory 16 GB G.Skill Flare X
Video Card(s) nVidia (Gigabyte) GT 740 2GB
Storage 80 GB IBM, 2 TB WD Green
Display(s) Dell 2007WFP
Software Debian Buster
#2
It's initialized to gibberish because that's what the memory was when the program took it for it's own use.
 

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
17,070 (3.44/day)
Likes
17,985
Processor Core i7-4790K
Memory 16 GB
Video Card(s) GTX 1080
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 7
#4
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
 
Joined
Jun 29, 2006
Messages
227 (0.05/day)
Likes
12
#5
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

Administrator
Staff member
Joined
May 14, 2004
Messages
17,070 (3.44/day)
Likes
17,985
Processor Core i7-4790K
Memory 16 GB
Video Card(s) GTX 1080
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 7
#6
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
 
Joined
Jun 29, 2006
Messages
227 (0.05/day)
Likes
12
#7
Would it still exist at runtime before it has been declared even if the variable was allocated to heap?
 
Joined
Sep 24, 2008
Messages
2,470 (0.73/day)
Likes
613
Location
Hillsboro, Oregon, USA
System Name Dire Wolf II
Processor Intel Core i7 7820HQ (2.9Ghz, up to 3.9Ghz)
Motherboard HP 8275
Memory 32GB DDR4 2400Mhz
Video Card(s) Sapphire R9 Fury Nitro OC 4GB (Thunderbolt3 eGPU), nVidia Quadro M1200 (GTX750Ti) 4GB GDDR5
Storage HP NVMe 256GB
Display(s) HP Z27q (5120x2880) + Dell P2715Q (3840x2160)
Case HP ZBook 15 G4
Audio Device(s) Musiland Monitor 02 US, Skullcandy SLYR
Power Supply 150W HP PSU (for Laptop) + Corsair RM550x (for eGPU)
Mouse Logitech G400
Keyboard Corsair K95 RGB
Software Windows 10 Enterprise 64-bit
#8
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.