Discussion:
[PIC] XC32 warning message
Neil
2018-09-15 16:07:27 UTC
Permalink
Hi all,

I've got bitmaps defined using const (to store it in program memory) in
my PIC32 code as...
const unsigned char Logo1Bitmap[312] =
{
0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xF0,
0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0,
...
}


I also have this function...
void DrawBitmap(signed short XPos, signed short YPos, signed short
width, signed short height, unsigned char *bitmap, unsigned short color);


When I pass the bitmap to the function like this...
DrawBitmap(85, 28, 52, 42, Logo1Bitmap, 0x7BEF);


I get warning messages like this...
warning: passing argument 5 of 'DrawBitmap' discards 'const' qualifier
from pointer target type [enabled by default]
DrawBitmap(85, 28, 52, 42, Logo1Bitmap, 0x7BEF);

The only way I can get the warning to go away is to do this...
DrawBitmap(85, 28, 52, 42, (unsigned char *)&Logo1Bitmap[0],
0x7BEF);


It works properly either way, but I'm unclear what it thinks I'm doing
wrong. And what's the right way to do this?

Cheers,
-Neil.
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Jason White
2018-09-15 17:53:06 UTC
Permalink
Change the function's declared type to "const unsigned char *bitmap"
Post by Neil
Hi all,
I've got bitmaps defined using const (to store it in program memory) in
my PIC32 code as...
const unsigned char Logo1Bitmap[312] =
{
0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xF0,
0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0,
...
}
I also have this function...
void DrawBitmap(signed short XPos, signed short YPos, signed short
width, signed short height, unsigned char *bitmap, unsigned short color);
When I pass the bitmap to the function like this...
DrawBitmap(85, 28, 52, 42, Logo1Bitmap, 0x7BEF);
I get warning messages like this...
warning: passing argument 5 of 'DrawBitmap' discards 'const' qualifier
from pointer target type [enabled by default]
DrawBitmap(85, 28, 52, 42, Logo1Bitmap, 0x7BEF);
The only way I can get the warning to go away is to do this...
DrawBitmap(85, 28, 52, 42, (unsigned char *)&Logo1Bitmap[0],
0x7BEF);
It works properly either way, but I'm unclear what it thinks I'm doing
wrong. And what's the right way to do this?
Cheers,
-Neil.
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
--
Jason White
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Neil
2018-09-16 03:57:23 UTC
Permalink
Got it (and from the other responses also). I was not aware that I
could use const as a parameter type in a function.

Thanks
Post by Jason White
Change the function's declared type to "const unsigned char *bitmap"
Post by Neil
Hi all,
I've got bitmaps defined using const (to store it in program memory) in
my PIC32 code as...
const unsigned char Logo1Bitmap[312] =
{
0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xF0,
0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0,
...
}
I also have this function...
void DrawBitmap(signed short XPos, signed short YPos, signed short
width, signed short height, unsigned char *bitmap, unsigned short color);
When I pass the bitmap to the function like this...
DrawBitmap(85, 28, 52, 42, Logo1Bitmap, 0x7BEF);
I get warning messages like this...
warning: passing argument 5 of 'DrawBitmap' discards 'const' qualifier
from pointer target type [enabled by default]
DrawBitmap(85, 28, 52, 42, Logo1Bitmap, 0x7BEF);
The only way I can get the warning to go away is to do this...
DrawBitmap(85, 28, 52, 42, (unsigned char *)&Logo1Bitmap[0],
0x7BEF);
It works properly either way, but I'm unclear what it thinks I'm doing
wrong. And what's the right way to do this?
Cheers,
-Neil.
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Joep Suijs
2018-09-15 17:58:36 UTC
Permalink
Hi,

The compilers warns you that you state (with const) your struct is not
allowed to be modified, but the DrawBitmap function apparently is defined
taking that parameter without 'const', hence not agreeing it won't change
the struct content.
If the DrawBitmap function is yours and it does not modify this parameter,
the best way to remove the warning is making the 5th parameter const.
Casting is okay too, since it shows you checked and found it is okay. I
would not recommend ignoring this (or any other) warning.

Joep
Post by Neil
Hi all,
I've got bitmaps defined using const (to store it in program memory) in
my PIC32 code as...
const unsigned char Logo1Bitmap[312] =
{
0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xF0,
0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0,
...
}
I also have this function...
void DrawBitmap(signed short XPos, signed short YPos, signed short
width, signed short height, unsigned char *bitmap, unsigned short color);
When I pass the bitmap to the function like this...
DrawBitmap(85, 28, 52, 42, Logo1Bitmap, 0x7BEF);
I get warning messages like this...
warning: passing argument 5 of 'DrawBitmap' discards 'const' qualifier
from pointer target type [enabled by default]
DrawBitmap(85, 28, 52, 42, Logo1Bitmap, 0x7BEF);
The only way I can get the warning to go away is to do this...
DrawBitmap(85, 28, 52, 42, (unsigned char *)&Logo1Bitmap[0],
0x7BEF);
It works properly either way, but I'm unclear what it thinks I'm doing
wrong. And what's the right way to do this?
Cheers,
-Neil.
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
smplx
2018-09-15 23:14:32 UTC
Permalink
Post by Neil
Hi all,
I've got bitmaps defined using const (to store it in program memory) in
my PIC32 code as...
const unsigned char Logo1Bitmap[312] =
{
0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xF0,
0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0,
...
}
I also have this function...
void DrawBitmap(signed short XPos, signed short YPos, signed short
width, signed short height, unsigned char *bitmap, unsigned short color);
When I pass the bitmap to the function like this...
DrawBitmap(85, 28, 52, 42, Logo1Bitmap, 0x7BEF);
I get warning messages like this...
warning: passing argument 5 of 'DrawBitmap' discards 'const' qualifier
from pointer target type [enabled by default]
DrawBitmap(85, 28, 52, 42, Logo1Bitmap, 0x7BEF);
The only way I can get the warning to go away is to do this...
DrawBitmap(85, 28, 52, 42, (unsigned char *)&Logo1Bitmap[0],
0x7BEF);
It works properly either way, but I'm unclear what it thinks I'm doing
wrong. And what's the right way to do this?
Think of it like this: the function has an inside and an outside. The
interface to the function (the formal arguments) tell the compiler how the
function will interact with the outside. The compiler does not actually
check the inside of the function when a function is called, it just takes
it for granted the interface adequatly describes the inside.

The compiler compiles the inside of the function seperatly from the
outside and relies on both sides obeying the definition of the interface.

On the inside, the function might actually use the stuff passed to it in
an unexpected manor, not consitant with the interface at all.

if you have a function
DrawBitmap(int x1, int y1, int x2, int y2, char *bitmap)

This actually tells the compiler DrawBitmap could modify the contents of
bitmap. It doesn't mater that DrawBitmap does not actually change the
contents of bitmap.

if you redefine the function as
DrawBitmap(int x1, int y1, int x2, int y2, const char *bitmap)

This tells the compiler that DrawBitmap WILL NOT modify the contents of
bitmap. When the compiler compiles the inside of the function it will
complain if it attempts to modify the contents of bitmap.

Be very VERY wary of casting to get you out of trouble. Some architecture
treat pointers to RAM as distinct from pointers to code space. This means
that although the casting might work perfectly well on one platform it
might fall over if you try to port your code to a different architecture
(e.g. PIC16 or a CPU with 16 bit ints instead of 32 bit) and leave you
with a hard to find bug.

One last word of warning about casting: it is evil, don't do it!!! No
seriously it's a beartrap waiting to bite you.

It basically says to the compiler, I don't care what you think, do this
anyway.

Consider the following

int fred(char *ptr)
{
*(float *)ptr = 3.1415;
}

#defne max_buff_len 32

char jack[max_buff_len];

fred(&jack);

Now imagine that "max_buff_len", "jack" and "fred" are defined somewhere
spread across several files and a few thousand lines of source code. What
happens when you change something that involves these 3 items. The
compiler wont flag an error if it spots as inconsistancy. You told it not
to by virtue of the cast.

Moral is: casts = bad.

Regards
Sergio Masci
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Loading...