このプログラムは、ポインター (8 バイト) と 3 つの int (それぞれ 4 バイト) を持つ構造体を作成して測定し、4 バイトのデータ パディングがあることを示します。
CPUが一度に8バイトを処理し、さらに8バイトのパディングが必要か、一度に4バイトを処理し、何もないはずです。
それとも、メモリの 8 バイト セクションに 2 つの 4 バイト値を貼り付けて、実行時に CPU に分割させますか? (これで不一致が説明されますが、私には少し非効率的です)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct test {
char *name;
int age;
int height;
int weight;
};
struct test *create(char *name, int age, int height, int weight)
{
struct test *thing = malloc(sizeof(struct test));
thing->name = strdup(name);
thing->age = age;
thing->height = height;
thing->weight = weight;
return thing;
}
void destroy(struct test *thing)
{
free(thing->name);
free(thing);
}
int main(int argc, char *argv[])
{
struct test * t1 = create("bleh",1,2,3);
printf("Sizeof struct: %lu\n",sizeof(struct test));
printf("Sizeof pointer (On 64bit system): %lu\n",sizeof(char *));
printf("Sizeof int: %lu\n",sizeof(int));
destroy(t1);
return 0;
}
出力:
Sizeof struct: 24
Sizeof pointer (On 64bit system): 8
Sizeof int: 4