C++基础--struct的大小

在修改别人的代码的过程中,发现很多人会把struct和struct的定义混淆,在这里主要是为了提醒自己Struct定义的规范性。

#include <stdio.h>

struct x{
	int a;
	char b;
};

typedef struct g{
	int a;
	char b;
}G;

int main()
{
	int a;
	char b;
	int m = sizeof(a);
	int n = sizeof(b);
	printf("size of int is %d
", m);
	printf("size of char is %d
", n);
	int x = sizeof(x);
	int y = sizeof(G);
	printf("size of struct is %d
", x);
	printf("size of struct G is %d
", y);
	return 0;
}

  运行的结果为:

在这里Struct G的大小为8是因为:字节对齐,说明Struct在默认情况下,就已经做了字节对齐。

原文地址:https://www.cnblogs.com/anlia/p/Struct.html