Minor issue with understanding

Posts: 458
Joined: 2002.09
Post: #16
There are two reasons why a variable might happen to be zero when uninitialized. It's not astronomically unlikely...

1) When things are compiled in debug mode, some C++ compilers will initialize memory to zero. Unfortunately when writing production software this means that the production code may behave differently from the debug code when an uninitialized variable is encountered, which delays the finding of any problems. After getting bitten by that once too often at my job, we have made it part of our code reviews to insist that variables always be initialized when they are declared, either from a constructor or (for simple types like int and pointers) explicitly. This quickly becomes second nature. (It also ensures that there aren't obscure paths through the code that leave a variable uninitialized.)

2) It can happen that, while not set explicitly, the nature of the code that runs prior to the variable being allocated on the stack or heap causes the memory to be initialized a certain way. Obviously, 0 has many special meanings (end of string, initial index, NULL pointer) that it shows up fairly often.

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Understanding multisampled FBOs Coyote 12 13,095 Nov 27, 2009 01:01 AM
Last Post: arekkusu
  Understanding glColorMaterial PhysicsGuy 3 8,738 Nov 25, 2008 06:25 PM
Last Post: PhysicsGuy
  glTexImage2D() & memory understanding... sealfin 4 5,052 Aug 7, 2004 03:15 PM
Last Post: OneSadCookie