View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
New compiler Warning |
Posted: Thu Apr 07, 2016 9:43 am |
|
|
I was using compiler 5.025 ok now I updated to 5.056 and new warnings appears.
Pointer types do not match
Code: | long long FlagsRules1;
long long FlagsRules2;
long long FlagsRules3;
char FlagsRules4;
#define EE_R99 3999
#define FlagsRules1_EE EE_R99 + 1
void WriteBufferEE(long StartAddress, char *DataBuffer, char ByteCount)
{
//....code here
}
void UpdateRuleX(char Num, short Enable)
{
if(Num<32)
{
if(enable)
{
bit_set(FlagsRules1,Num);
}
else
{
bit_clear(FlagsRules1,Num);
}
WriteBufferEE(FlagsRules1_EE,&FlagsRules1,4);//<--- Warning HERE
}
else if(Num<64)
{
//More code here..
}
}
|
Can somebody tell me what I'm doing wrong? _________________ Electric Blue |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 07, 2016 10:14 am |
|
|
Well. look at that line. You told the compiler that the function accepts
the address of a char pointer. Then you give it a parameter (the middle
one), that is the address of a 'long long'. How does the compiler know
that your 'long long' is a char buffer ? It doesn't. If you want to do that,
you have to tell the compiler what you're doing. This is done with a cast:
Quote: | WriteBufferEE(FlagsRules1_EE, &(char *)FlagsRules1, 4); |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Apr 07, 2016 11:09 am |
|
|
and, checking for this type of problem, has been successively tightened on the last few compiler versions, so 'more warnings' will occur on older code. |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Thu Apr 07, 2016 1:07 pm |
|
|
Ok, now I changed the code to
Code: | WriteBufferEE(FlagsRules1_EE,&(char)(FlagsRules1),4); |
But generates the same amount of instructions
Do I still wrong?
Does it worth to explicitly make a cast? Because seem like the compiler was already casting to char. _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Apr 07, 2016 2:13 pm |
|
|
First, it is a warning. These tell you something _may_ be wrong, not that it is.
Then, why should the number of instructions change?.
The key point about 'types' with pointers, is that these tell the compiler how _big_ something is. If you have a char pointer, and increment it, it moves forwards by one 'char' sized lump. If you increment a pointer to a 'long', it instead moves forward by a 'long' sized lump.
Now, your buffer 'write' code, may well internally always treat the pointers as 'char' sized, but if you hand to this a pointer to something of a different size, then you should be made aware that the sizes used in the function, _may not_ be the sizes you expect. The cast says to the compiler "I understand that I am actually going to deal with this in bytes, so don't warn me".
There are many occasions where warnings can be ignored. However, In a few months time, you might be dealing with a function, where you actually _want_ to handle the variable in larger pieces, and then the warning would say 'look out' the size being used in this function, is _not_ the one you expect.... |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Thu Apr 07, 2016 3:07 pm |
|
|
Ok, so, Whats about this?
Code: |
#define APN1 Vector5 + 53//
char APN_Offset,APN_Len;
WriteBufferEE(APN1,APN_Offset,APN_Len); |
Both, APN_Offset and APN_Len are char type, so Why is warning me? _________________ Electric Blue |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Thu Apr 07, 2016 3:24 pm |
|
|
Because you are giving it a char when it expects a pointer to a char |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
|
Posted: Thu Apr 07, 2016 5:04 pm |
|
|
And in that case as it would lead to a horrible bug ( passing in a char when a pointer is expected ). You would be accessing memory that is not intended to be accessed. |
|
|
|