One of this is warning for uninitialzed variables.
Let's see the following code:
typedef union
{
uint8_t value1;
uint8_t value2;
} demostruct;
.
.
.
demostruct uninited;
int i;
for (i = 0; i < uninited.value1; i++)
{
//do something
}
You'll probably be in some serious trouble running this code, as whatever you do in the for loop, the execution of it surely won't be as you imagined.
You'll probably think you won't fall into this, as you're a good coder.
Well I thought the same until now.
I simply rewrote my code, and forgot the now uninited variable in the for loop. So something that previosly worked, now did some silly things.
How could I have avoided this? Well maybe if GCC or xc16 as told me that that variable is uninitialized.
Well, xc16 compiles with the -Wall option, which tells the compiler to show all warnings. There is only a slight problem:
Well, I usually develop my code with -O0 optimization, since it makes debugging the easiest. It's really a pitty, that this warning does not work doing this.
After a little investigation Iit turns out, that the compiler would have been able to warn me, if the the uninitialized variable had not been a struct.
The xc16 would have been able to warn me using this code:
int uninited;
int i;
for (i = 0; i < uninited; i++)
{
//do something
}
But what about the other embedded compilers out there?
Under the term: Highlight in IDE, I mean the following:
This is what you normally see in an eclipse based IDE.
In Atmel Studio, which is Visual Studio based, you get a good overviewable error list:
Sadly in Mplab X, you'll have to watch closely to see your compiler warnings, you have to scroll through a lot of unimportant messages:
So to get all possible flaws in your code, you'll best of, if you compile with the appropriate settings.
No comments:
Post a Comment