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

c++ compiler optimizations

Joined
Jun 29, 2006
Messages
230 (0.04/day)
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,850 (0.36/day)
System Name Eldritch
Processor AMD Ryzen 5 5800X3D
Motherboard ASUS TUF X570 Pro Wifi
Cooling Satan's butthole after going to Taco Bell
Memory 64 GB G.Skill TridentZ
Video Card(s) Vega 56
Storage 6*8TB Western Digital Blues in RAID 6, 2*512 GB Samsung 960 Pros
Display(s) Acer CB281HK
Case Phanteks Enthoo Pro PH-ES614P_BK
Audio Device(s) ASUS Xonar DX
Power Supply EVGA Supernova 750 G2
Mouse Razer Viper 8K
Software Debian Bullseye
It's initialized to gibberish because that's what the memory was when the program took it for it's own use.
 
Joined
Jun 29, 2006
Messages
230 (0.04/day)
but are all variables initialized to something even before their statement?
 

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
27,028 (3.71/day)
Processor Ryzen 7 5700X
Memory 48 GB
Video Card(s) RTX 4080
Storage 2x HDD RAID 1, 3x M.2 NVMe
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 10 64-bit
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
230 (0.04/day)
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
27,028 (3.71/day)
Processor Ryzen 7 5700X
Memory 48 GB
Video Card(s) RTX 4080
Storage 2x HDD RAID 1, 3x M.2 NVMe
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 10 64-bit
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
230 (0.04/day)
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,665 (0.47/day)
System Name Dire Wolf IV
Processor Intel Core i9 14900K
Motherboard Asus ROG STRIX Z790-I GAMING WIFI
Cooling Arctic Liquid Freezer II 280
Memory 2x24GB Corsair DDR5 6667
Video Card(s) NVIDIA RTX4080 FE
Storage AORUS Gen4 7300 1TB + Western Digital SN750 500GB
Display(s) Alienware AW3423DWF (QD-OLED, 3440x1440, 165hz)
Case Corsair Airflow 2000D
Power Supply Corsair SF1000L
Mouse Razer Deathadder Essential
Keyboard Chuangquan CQ84
Software Windows 11 Professional
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.
 
Top