Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struct pointer cast as uint8_t * throws an error

in my function I allocate memory for and fill a structure called messagePacket

struct messagePacket *packet = malloc(sizeof(struct messagePacket));
//fill

When I try to cast the pointer as a (uint8_t *), gcc throws a warning that says: large integer implicitly truncated to unsigned type

sendBuf(..., (uint8_t *)packet);

I've been able to do the following just fine, and I understand I can use this approach as a workaround. I'm here because I would rather learn from this than work around it.

uint8_t *buf = malloc(sizeof(struct messagePacket));

The size of struct messagePacket = 1209 B. My best guess is that the chunk of memory is super large that I gets stored in a high memory address, such as a 16 bye address? But that doesn't fit with the fact that I can malloc a uint8_t * of the same size.

like image 785
austin Avatar asked May 17 '26 06:05

austin


1 Answers

I guess the compiler notices that your struct is larger than 8bit and using uint8_t you will only address the first byte of the structure. Since this seems to be intended you could cast to (void *) and then to (uint8_t *). But you should tell sendBuf the buffer size which is sizeof(struct messagePacket).

like image 112
konqi Avatar answered May 20 '26 00:05

konqi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!