AW: Re: [Icc-avr] Bootloader problem PROBLEM FOUND
David Brown
david_brown at hotpop.com
Mon Nov 5 06:22:35 PST 2007
Daub, Stephan wrote:
> Hi all,
>
>> You must do this explicitly in your >code.
> ...But is this good programming style anyway (in all programming
> languages), isn't it ?
>
> Best, stephan
>
It is always important to initialise your local variables before using
them - using a variable before it has a value assigned is not a matter
of style, but a matter of correct code. However, there is no need to
explicitly initialise a variable if it is latter set before using it:
void foo(void) {
int i = 0; // unnecessary explicit initialisation
for (i = 0; i < 10; i++) ...
}
For non-static local variables, there is no difference between giving a
value when the variable is declared, and a simple assignment. It is
therefore often convenient to assign the initial value just before the
variable is first used, rather than where it is declared - this may also
lead to better code generation (on C99 compilers, you may want to leave
the declaration until later as well, but that's a matter of style).
It is also unnecessary to explicitly initialise file-scope data that
should start at 0 - this is done automatically, and explicit
initialisation is a waste of space. There are exceptions - if you have
an enumerated type, or a variable normally used with a set of #define'd
constants, you should probably explicitly initialise it with the
enumeration value even if that is represented by 0.
mvh.,
David
> -----Original Message----- From: icc-avr-bounces at imagecraft.com
> <icc-avr-bounces at imagecraft.com> To: Johan Wallström <johan at edab.nu>;
> Discussion list for ICCAVR and ICCtiny Users. You do NOT need
> tosubscribe to icc-announce if you are a member of this.
> <icc-avr at imagecraft.com> Sent: Mon Nov 05 12:17:34 2007 Subject: Re:
> [Icc-avr] Bootloader problem PROBLEM FOUND
>
> And the global/static variables only gets initialized ONCE (at
> program start-up). No variables are initialized automagically upon
> function entry (except the argument(s) of course). You must do this
> explicitly in your code.
>
> Regards Johan Bodin
>
> ----
>
> Johan Wallström wrote:
>>> If I don't initialize the TimeOut = 0; the code didn't work
>>> (that's in bootloader application)
>>>
>>> Sylvain
>> Yeah, (uninitialized) global and static data get's initialized to
>> zero by the compiler, while (uninitialized) local data does not -
>> they get whatever value happens to be there.
>>
>> regards Johan Wallström Elektronik Design AB +4660169550
>>
More information about the Icc-avr
mailing list