SV: SV: [Icc-avr] Stack problems
Bengt Ragnemalm
benra at imt.liu.se
Mon Aug 18 00:48:02 PDT 2008
Yes, you are absolutely right. So the programmer must have good knowledge of
about how long, how thick and the material strength of the rope? Damn, and I
who thought I used a ladder. (C=rope, Pascal=ladder, Basic=elevator, brick
wall=assembler???)
A question: Does this mean that the compiler never have any control over if
the RAM used for local variables by a single function are more than total
RAM-global variables? This is what happened. I couldn't see how this can be
such a big deal for the compiler.
The code actually runs fine even after the "crash", it depends on what
registers are overwritten (of course). It would have been easier to find if
it did not. The reason for that the Stack Checking function didn't find
anything was that the code at the time of using the 512 byte string in one
step moved the RAM usage outside the RAM. And as the program didn't filled
the entire string the Stack Checking bytes was never overwritten. It would
of course resulted in bigger problems in the future as more of the string
will be used.
Maybe it could be that the compiler have problem of freeing the RAM space
for the first array before the RAM space for the second array are used. A
local array should be to prefer as it would use less RAM in the total
application but a global may be necessary if that is the only way to see
that the function defines to much RAM.
The code is something like this:
void Function(void)
{
unsigned char array1[512];
unsigned char array2[512];
unsigned char sendData;
...
for(...)
{
array1[...]=...; // Reading values to array 1
array1[...]=...;
...
}
if (globalFlag)
{
for(...)
{
sendData = array1[...];
}
else
{
SendArray(array1); // Last usage of array 1
array2[...]=; // Some usage of array 2
array2[...]=;
...
SendArray(array2); // Last usage of array 2
}
}
/Bengt
> -----Ursprungligt meddelande-----
> Från: icc-avr-bounces at imagecraft.com [mailto:icc-avr-
> bounces at imagecraft.com] För Richard Man
> Skickat: den 15 augusti 2008 20:37
> Till: icc-avr at imagecraft.com
> Ämne: Re: SV: [Icc-avr] Stack problems
>
> Bengt, the usual joke is that C gives you enough rope to hang yourself. To
> wit:
>
> foo() { char a[10];
> bar(a);
> }
>
> in a different .c file
>
> bar(char *a) {
> int i;
> ...
> a[i] = ...
> }
>
> Where does a[i] write to? Well, it depends on "i" and "i" can be
> anything, a valid index from 0 to 9, or -1000, or 10000. When i is
> invalid, surely it chomps on some memory somewhere. It could be your
> IO registers, your stack, your data, your CPU register....
>
> At 04:25 AM 8/15/2008, Bengt Ragnemalm wrote:
> >Sorry, this was of course the same reason that before, the big local
> array.
> >After changing into one global this problem disappeared. But maybe
> someone
> >could describe this a little so I understand what happened? The stack
> >checking bytes was not overwritten.
>
> // richard (This email is for mailing lists. To reach me directly,
> please use richard at imagecraft.com)
>
> _______________________________________________
> Icc-avr mailing list
> Icc-avr at imagecraft.com
> http://dragonsgate.net/mailman/listinfo/icc-avr
More information about the Icc-avr
mailing list