Weird boolean array thing O_o

Feb 3, 2012 at 12:27pm
Erm, I came across a weird problem with boolean arrays, if you declare bool array globally, its whole content is automatically set to false (0s), however when u declare that array inside any function then its values are screwed up, i mean some of them are 0s, some are completely random, why is that ?
Feb 3, 2012 at 1:05pm
It is the same for any variable, not just arrays of bool. Statics are zero-initialized.
Feb 3, 2012 at 1:09pm
Objects with static storage duration, such as globals, are always zero-initialized by default. When you declare the array inside a function it will not be initialized by default so that's why you get garbage values. If you want to zero initialize the array you can just initialize it with an empty pair of curly brackets.

bool boolArray[100] = {};
Feb 3, 2012 at 2:11pm
Generally, you should not depend on anything having useful values that isn't explicitly initialized (well, except for default-constructed objects).
Feb 3, 2012 at 3:47pm
You should read this set of slides: (a few lines down)

http://www.slideshare.net/olvemaudal/deep-c

This part of the language gets explicitly addressed.
Last edited on Feb 3, 2012 at 3:50pm
Topic archived. No new replies allowed.