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

sizeof struct

I have a struct with 2 members of type double and 1 member of type long.

The sizeof() function returns 24. Normaly it should be 20(2*8bytes for the doubles and 4 bytes for the long).

When I add a another member of type long sizeof() returns 32.

Another struct with 3 longs returns 12 with sizeof().

I suppose it has something to do with allignment and optimalisation? How can I avoid this.

Luc Vercruysse

Parents
  • "on an ARM is it generally more efficient to leave the smallest structure elements for the end of the structure"

    The general case for any processor that can take advantage of alignment is that you get the smallest and most efficient structures if the data is grouped on increasing or decreasing align requirements.

    one long and one short and two byte can be grouped into 8 bytes if the align sizes are sorted in increasing or decreasing order. Any other combination will result in extra padding.

Reply
  • "on an ARM is it generally more efficient to leave the smallest structure elements for the end of the structure"

    The general case for any processor that can take advantage of alignment is that you get the smallest and most efficient structures if the data is grouped on increasing or decreasing align requirements.

    one long and one short and two byte can be grouped into 8 bytes if the align sizes are sorted in increasing or decreasing order. Any other combination will result in extra padding.

Children