Weird boolean array thing O_o

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 ?
It is the same for any variable, not just arrays of bool. Statics are zero-initialized.
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] = {};
Generally, you should not depend on anything having useful values that isn't explicitly initialized (well, except for default-constructed objects).
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
Topic archived. No new replies allowed.