int shown as to take 8 bytes [duplicate]

一笑奈何 提交于 2021-01-29 18:13:34

问题


I have taken code from https://www.geeksforgeeks.org/the-offsetof-macro/.

I ran the code in ide provided in gfg itself.

I have edited code a bit , sizeof(int) is showing 4 but it is shown to take 8 bytes in struct through offset

#include <stdio.h> 

#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT)) 

typedef struct PodTag 
{ 
    int   i; 
    double d; 
    char  c; 
} PodType; 

int main() 
{ 
    printf("%ld", OFFSETOF(PodType, d) ); 

    getchar(); 
    return 0; 
} 

回答1:


The next field d requires 8 byte alignment. This means that 4 padding bytes are inserted between the fields.

On other architectures it might be different.



来源:https://stackoverflow.com/questions/54940026/int-shown-as-to-take-8-bytes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!