1

In the project I'm working on Xcode automatically initializes variables to 0 on startup/ at compile time, when I start the debug build. At least everything looks like it.

I'm often running into the problem, that I haven't initialized a variable to NULL - which works fine on my Mac - and when someone else compiles and starts the project (especially on Windows machines, as it's a multiplatform project), they get a EXC_BAD_ACCESS, because == NULL is false.

I'm pretty sure there exists a setting to turn this behavior off, or is it just coincidental, that the allocated memory is always fresh on my machine?

v01pe
  • 1,096
  • 2
  • 11
  • 19
  • 1
    if you're doing cross compiling code, it sounds like *you* need to be more careful about writing code that accesses variables before they get initialized. – Michael Dautermann Feb 16 '12 at 12:02
  • Of course I have to take case of it! But when I forget about it, it's working fine on my machine and the others find the bug, which is annoying and I want to prevent that if possible! Of course I have to init that stuff! – v01pe Feb 16 '12 at 13:03

1 Answers1

3

It is likely just coincidental.

On about every modern multi user OS, when the OS gives new memory to the process, it erases all old contents, to not have information leak from one process to the other. So when you access some memory the first time, it apepars as if it is set to 0.

This will happen in certain situations more likely when using debug builds, since optimization often includes lowering the stack footprint, thus is reusing certain memory much earlier.

PlasmaHH
  • 15,673
  • 5
  • 44
  • 57
  • Hmm, OK starange, as it appears to be never happening on my machine, and always on the other machines. But we probably just never stumbled upon the other case. Thanks though! – v01pe Feb 16 '12 at 13:02