Advertisement

dumb question time

Started by September 04, 2000 09:30 PM
13 comments, last by Bino 24 years, 4 months ago
quote: Original post by Dormeur
Oh, there is one special kind of variable that is always initialized to 0 at startup, and that is a static variable. If it wasn''t, it wouldn''t be of much use (except as global statics). Here''s some code that really needs that feature (although it doesn''t do very much ):


I don''t know if all compilers initialize static variables to zero either. But you can write as follows, and the variable is initialized only once:

    static int zero = 0;    


-Jussi
Standardly all globals and local static variables are set to 0 when declared, all non-static locals get whatever was already in that memory area when they are declared.
Either way it''s good programming practice to initialise the variables yourself, global, local or static. Don''t rely on others to do your work for you, or if you do, check it for _your_ compiler.

"Assumption is the mother of all f*ck ups."

Mike
Advertisement
Neuro, Microsoft had to do that.
By doing this in debug mode, it enables you to actually view a variable when you are debuging, whether initialized or not.
In release mode, it''s not initialized because it''s possible that the developer does not want it to happen. ( e.g. Slightly slower code.)
If you don''t initialize it, it have a NULL value, which could be anything. Kind of like if you don''t ZeroMemory() your DirectDraw surfaces, you''ll get all kinds of strange old memory crap on your screen.
Java sets many datatypes to 0 by default, but not all {and it is not a good idea to rely on it.}

Director {Shockwave} cannot initailize a variable without a value, as the only way to tell a variable it''s datatype is by giving it a value. Kind of confusing at first, but it beats Hungarian Notation.

This topic is closed to new replies.

Advertisement