Altera_Forum
Honored Contributor
21 years agostructure alignement problem ?
I had declared a buffer to read the contents of a flash block as following
BYTE buffer[0x10000]; this buffer should content an array of a structure defined as following # define BYTE alt_u8# define WORD alt_u16# define DWORD alt_u32 typedef struct s_partition_record { DWORD magig; WORD boot; WORD type; char name[33]; char version[9]; DWORD startblock; DWORD endblock; DWORD cts; WORD crc; WORD record_crc; BYTE pad[62]; } PARTITIONRECORD; to get a pointer to the first structure I used the following int recordstartadd; PARTITIONRECORD *pr_temp; recordstartadd=recno*sizeof(PARTITIONRECORD); pr_temp=(PARTITIONRECORD*)(buffer+recordstartadd); I made a printf for value pr_temp->magig, which should be 0x5a0F5A0F but it returned only 0x5A0F0000 ! this seems to be some alignement problem, I checked the buffer pointer and it was aligned to 16 bit. I changed the buffer declaration to BYTE __attribute__((aligned(4))) buffer1[0x10000]; and from now on, printf returned the correct value 0x5a0F5A0F My question is, is the user responsible to pay attention of the alignement or is this normally a job of the compiler. Why is the buffer not automatically aligned at 32bit.