cancel
Showing results for 
Search instead for 
Did you mean: 

8bit in struct is allocated 32bit in memory

victagayun
Senior III

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?

1 ACCEPTED SOLUTION

Accepted Solutions
S.Ma
Principal

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....

View solution in original post

2 REPLIES 2
S.Ma
Principal

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....