This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Structure Size

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

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

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

Children