问题
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