??? 03/05/10 15:50 Read: times Msg Score: +1 +1 Good Answer/Helpful |
#173882 - Auro variables are always undefined Responding to: ???'s previous message |
Andy Neil said:
You cannot rely on all variables to be initialised by startup code.
If you try to remember which ones are, and which are not, initialised by the startup code - sooner or later you will get it wrong. Hence the safest rule is to insist that all variables must be explicitly iniialised (or assigned a vlaue) before use. void my_function( void ) { u8 uninitilised; u8 initialised = 0; u8 assigned; : assigned = something; // now we can safely do stuff that reads 'assigned'... } The C language defines that static and global variables are zero if not explicitly initialised. Auto variables are NOT defined. So Andy's example is totally valid. Most modern compilers will warn you about use of auto variables, but they do not have to. There is absolutely no point in zeroing un-explicit global variables. The C startup code should have done it for you. There is no requirement to zero any dynamic memory. So you should always initialise malloc()'ed memory or use calloc() in the first place. Special Function Registers contents will all depend on whether they have come from a Reset or not. The data sheet should explain the actual contents. The C standard knows nothing about SFR's. They are just volatile memory. Now if you choose use non-standard Compiler extensions to destroy the essentials of the language, the ball is in your own court. David. |