cancel
Showing results for 
Search instead for 
Did you mean: 

Compiler Bug?

jgauthier
Senior

I ran into a warning for one section of code that I did not for a different section of code. So I simplified it into two function. I don't see why I get a warning for one and not the other.

void test32(uint32_t *data) {
	uint32_t src_addr = (uint32_t) data;
}
 
void test16(uint16_t *data) {
	uint16_t src_addr = (uint16_t) data;
}

The compiler complains (also about unused variables, but I removed those)

../Core/Src/main.c: In function 'test16':
../Core/Src/main.c:161:22: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  161 |  uint16_t src_addr = (uint16_t) data;
      |                      ^
 

The compiler appears to be based on gcc 9.3.1.

If I put these two functions in a simple C file and build it on straight Linux (gcc 9.3.0) the warning exists for both instances.

main.c: In function ‘test16’:
main.c:15:23: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  15 |  uint16_t src_addr = (uint16_t) data;
   |            ^
main.c: In function ‘test32’:
main.c:20:23: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  20 |  uint32_t src_addr = (uint32_t) data;
   |            ^
 

5 REPLIES 5
KnarfB
Principal III

64-bit Linux? This would explain the difference.

hth

KnarfB

Yes, x64 linux. I don't understand the difference between the two variables types on stm32, and why that warning comes up.

KnarfB
Principal III

sizeof a pointer is 8 bytes on a 64-bit Linux but 4 bytes on a 32-bit STM. Therefore test32 yields a the warning under Linux but not on STM (same size like uint32_t).

I see. It's the pointer size. Okay, thanks!

And for detail test16 is failure ... truncate part from pointer.