Author Options:

Possible bug in avr-gcc, any second opinions? Answered

I'm fairly sure that the following is a bug in AVR-GCC, but I'm not sure. Basically sizeof should tell you the size in memory of something in memory. So you can do sizeof(char) or sizeof(myArray) and it will tell you how many bytes are taken up. If I do sizeof(myArray) where myArray is a local array of chars the answer is correct. If I pass the value as a parameter then the result of sizeof(myArray) is always 1.

Does anyone have any idea's or explainaitions? This code is being put on an Attiny2313.
Thanks in advance,



Best Answer 8 years ago

If by 'parameter' you mean you are passing a pointer to the array then result is also correct, because it's telling you the size of the reference, not the size of the thing it points to.

For a character array you can walk the array yourself until you find the terminating \0 and count how many characters you found, but for other types of arrays you generally have to just restructure the code to find a way to avoid trying to do that...


Answer 8 years ago

Exactly. What gets passed is a pointer. Since array access and pointer math are aliases of one another, you don't normally notice this, but that's what's happening.

If you want to know the size, pass that separately. Or use a data structure which carries the size -- put that value in the first slot of the array, for example.


Answer 8 years ago

I suspect your right that it was a pointer issue. I have avoided this problem and done as you recommended, simply get the data out when allocating the memory. It could well have been any one of a number of issues, at the time I was riding the limit of available memory while not being careful about what I was doing.

Thanks for your time


8 years ago

I'd post a code snippet here, to help. It could be several issues...

Are you using "volatile" parameters and variables?

Because memory is so precious on microcontrollers, AVR-GCC is even more persnickety than normal C, especially when memory allocation is concerned.

Also, don't forget that local variables don't even exist outside of the function in normal C, let alone avr-gcc. They are "undefined" (usually stack-based).

I don't know if your memory is reallocated just before passing the value or not. You could assign the value to a local variable, and then try passing that... Either way, if the local array isn't "static volatile" then it doesn't exist outside the function anyways...