avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] unexpected compiler behaviour


From: Joerg Wunsch
Subject: Re: [avr-gcc-list] unexpected compiler behaviour
Date: Tue, 3 Sep 2002 13:19:10 +0200 (MET DST)

Chris Baugher <address@hidden> wrote:

> So if I do
> something like:
> 
>       unsigned char val[3];
> 
>       val[0] = '0';
>       val[1] = '1';
>       val[2] = '2';
> 
>       lcd_putstring(val, 3);
> 
> it works just fine.  But if I do:
> 
>         unsigned char val[3] = { '0', '1', '2' };
> 
>         lcd_putstring(val, 3);
> 
> I get the wrong characters on the display.  Are initialisations like this
> not allowed?

That's surely supposed to work.  Can you verify the generated
assembler code?

The way it works is quite different depending on whether the val[]
array is a global/static or local (auto) variable.  In the global
case, the values just get assigned to the .data segment, and the code
in gcrt1.S takes care of moving them from flash to SRAM before
starting your main().

If the variable is local to your function, those data still take space
in the .data section (so the same as above will happend at program
startup time, i. e. the values consume both flash and SRAM), but the
assignment to the RAM in your local stack fram happens in a loop when
entering the function.  Needless to say, you're wasting even more
memory with this variant (your explicit assignment of the array
elements would even waste less).

For larger arrays, the usual way out of this dilemma is to use
constant strings directly from program memory
(__attribute__((progmem))), but then your lcd_putstring() function
needs to be written differently.  Perhaps it would make some sense to
write a second lcd_putstring_p() for this purpose.

-- 
J"org Wunsch                                           Unix support engineer
address@hidden        http://www.interface-systems.de/~j/
avr-gcc-list at http://avr1.org



reply via email to

[Prev in Thread] Current Thread [Next in Thread]