We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi....I am writing code for STR710FZ2 using Keil compiler.when i checked the sizeof a structure which i defined i got the size as 12 eventhough the actual size is 10.Since arm is 32 bit i can only defines structure whose size is a multiply of 4...can anybody tell me a way to overcome this problem....
A large number of processors require that integers larger than one byte must be aligned corresponding to their size, i.e. a 16-bit integer mut be aligned on an even address, and a 32-bit integer must be aligned in an address evenly dividable by 4.
If you have a struct containing one 32-bit integer and one 16-bit integer, the used size may be 4+2 = 6 bytes. However, to be able to create arrays of such a struct, the struct must be padded to 8 bytes or the 32-bit integer in the following struct would not be correctly aligned, i.e.:
struct { u32 a; u16 b; } my_array[4];
will result in the following:
offset value 0 my_array[0].a 4 my_array[0].b 6 2-byte padding 8 my_array[1].a 12 my_array[1].b 14 2-byte padding 16 my_array[2].a ...
In your case, you have a struct that contains at least one 32-bit integer. Therefore, the struct will have a size that is 4*n byte large.
To overcome the problem use the __packed attribute. More info: http://www.keil.com/support/man/docs/armcc/armcc_cjajddhb.htm