本文主要是介绍17.5结构体字节对齐:为了效率,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*
* 结构体字节对齐:为了效率
* 1,结构体的大小一定是最大基本类型的整数倍
* 2,一般是以最大的基本类型为参照,进行对齐
* 3,成员的地址一定是自身大小的整数倍
*/
#include<stdio.h>
/*
* 结构体字节对齐:为了效率
* 1,结构体的大小一定是最大基本类型的整数倍
* 2,一般是以最大的基本类型为参照,进行对齐
* 3,成员的地址一定是自身大小的整数倍
*/struct Int
{int number;
}; //4个字节struct DBInt
{int number1;int number2;
}; //8个字节typedef struct
{short num1; //2char sex; //1
}Int32; //4个字节typedef struct
{char c; //1short s; //2char cc; //1 6 8 10 12
}Other; //6个字节typedef struct
{char c; //1 char cc; //1 short s; //2
}Other1; //4个字节struct Array
{char name[10]; //10 2*4=8 2short i; //2 int age; //4
}; //16个字节int main()
{//struct Int 有几个字节printf("%d\n", sizeof(struct Int));printf("%d\n", sizeof(struct DBInt));printf("%d\n", sizeof(Int32));printf("%d\n", sizeof(Other));printf("%d\n", sizeof(Other1));printf("%d\n", sizeof(struct Array));return 0;
}
这篇关于17.5结构体字节对齐:为了效率的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!