memory allocation for structures elements

前端 未结 7 1182
故里飘歌
故里飘歌 2021-01-19 05:14

Hi I am having difficulties in understanding about how the memory is allocated to the structure elements.

For example if i have the below structure and the size of c

7条回答
  •  悲哀的现实
    2021-01-19 05:51

    This is a compiler dependent behavior. Some compiler makes that 'double' to be stored after 8 bit offset.

    IF you modify the structure as below you will get different result.

    struct temp
    {
    double b;  // size is 8
    int a;     // size is 4
    int e;     // size is 4
    double d;  // size is 8
    char c;    // size is 4
    }
    

    Every programmer should know what padding you compiler is doing. E.g. If you are working on ARM platform and you set compiler settings to do not pad structure elements[ then accessing structure elements through pointers may generate 'odd' address for which processor generates an exception.

提交回复
热议问题