Skip to main content
victagayun
Senior III
November 23, 2022
Solved

8bit in struct is allocated 32bit in memory

  • November 23, 2022
  • 2 replies
  • 1305 views

Hello,

I am a newbie in struct.

I have a struct declaration below.

typedef struct __serial_data
{
	uint8_t frameStart;
	uint32_t data;
} serial_data;
 
serial_data mydata[25];

but frameStart is allocated 32bit in memory and this causes trouble when sending through UART.

Initialize the struct.

for(uint16_t cntr = 0; cntr < 25; cntr++)
 {
	 mydata[cntr].frameStart = 0xAA;
	 mydata[cntr].data = cntr + 1;
 }

Memory watch

0693W00000WJH75QAH.png 

Send to UART

0693W00000WJH7FQAX.png 

Memory location

0693W00000WJH7ZQAX.png 

Is there any way to send data like:

AA, 01, 00, 00, 00,
AA, 02, 00, 00, 00,
AA, 03, 00, 00, 00,
AA, 04, 00, 00, 00,
......

Where:

AA = frame start (8bits)

01 = data (32bits)

Or the only way to do it is to make a separate array and arrange it accordingly and manually?

This topic has been closed for replies.
Best answer by S.Ma

32 bit data are typically 4 byte modulo address, so is the beginning of a variable. Start by declaring a 5 byte array and fill it up manually. Otherwise, your code will be affected from little or big endianess that might bite you back in the future....

2 replies

Piranha
Principal III
November 23, 2022
S.Ma
S.MaBest answer
Principal
November 23, 2022

32 bit data are typically 4 byte modulo address, so is the beginning of a variable. Start by declaring a 5 byte array and fill it up manually. Otherwise, your code will be affected from little or big endianess that might bite you back in the future....