2024年2月8日发(作者:)
一.基本数据的长度
char:1(有符号无符号同)
short:2(有符号无符号同)
int:4(有符号无符号同)
long:4(有符号无符号同)
float:4
double:8
二.对齐的作用和原因:
各个硬件平台对存储空间的处理上有很大的不同。一些平台对某些特定类型的数据只能从某些特定地址开始存取。比如有些架构的CPU在访问 一个没有进行对齐的变量的时候会发生错误,那么在这种架构下编程必须保证字节对齐.其他平台可能没有这种情况,但是最常见的是如果不按照适合其平台要求对 数据存放进行对齐,会在存取效率上带来损失。比如有些平台每次读都是从偶地址开始,如果一个int型(假设为32位系统)如果存放在偶地址开始的地方,那 么一个读周期就可以读出这32bit,而如果存放在奇地址开始的地方,就需要2个读周期,并对两次读出的结果的高低字节进行拼凑才能得到该32bit数 据。显然在读取效率上下降很多。
三.一些常见的和需要注意的sizeof
char* a= "abc"; //sizeof(a)=4
==============================================
char b[100]; //sizeof(b)=100;
==============================================
char* c=(char*)malloc(100); //sizeof(c)=100
==============================================
char d[] = "abc"; //sizeof(d)=4
==============================================
int e[100]; //求数组的元素个数int c2 = sizeof( e ) / sizeof( e[0] ); // 总长度/第一个元素的长度
==============================================
void foo3(char f[3])
{
int c3 = sizeof( f); // c3 =4
}
==============================================
void foo4(char g[])
{
int c4 = sizeof( g); // c4 =4
}
==============================================
int a = 1, b =2; 执行sizeof (a = b)
之后为什么a=1, 而不是2. 为什么没有执行赋值操作? (淘宝笔试题)
(1)sizeof (type-name) 是求变量类型的大小 ,在编译期执行 ,因为sizeof 是在编译期内执行而不是在运行期 所以 sizeof (a = b) 是不会运行a = b 这个操作的。
(2)sizeof是一个运算符而不是一个函数。 编译器会查符号表求出值大小,而不会执行里面的表达式
(3)sizeof 是如何来看待 a = b的?
sizeof 是按照类型计算字节的,与值无关。 在编译器中,上句等价于 sizeof( int
= int ),
结果按照“左值”计算字节数
你可以试试
int a=1;
double b=2.0;
cout < cout < 一个结果是 4,一个是8 (4)sizeof(1) 1 是int类型 所以4 sizeof(1.1) 1.1 为double 所以8 当然这个根据机器不同结果也不一样 四.结构体和类的规则 1.数据类型自身的对齐值: 对于char型数据,其自身对齐值为1,对于short型为2,对于int,float,double类型,其自身对齐值为4,单位字节。 2.结构体或者类的自身对齐值:其成员中自身对齐值最大的那个值。 3.指定对齐值:#pragma pack (value)时的指定对齐值value。 4.数据成员、结构体和类的有效对齐值:自身对齐值和指定对齐值中小的那个值。 上面包含两层意思,一是结构体(类)里面的成员地址要按照自身的大小对齐,也就是 addressof()%sizeof()=0;二是整个结构体(类)要按照成员中自身对齐值最大 (N)的那个值进行对齐,也就是sizeof(struct)%N=0。 五.例子 1. 2. 3. 4. 5. 6. 7. 8. 9. #include "stdio.h" typedef struct { char a; // 1 byte int b; // 4 bytes short c; // 2 bytes char d; // 1 byte } struct_a; typedef struct { 10. char a; // 1 byte 11. char _pad0[3]; // padding to put 'b' on 4-byte boundary 12. int b; // 4 bytes 13. short c; // 2 bytes 14. char d; // 1 byte 15. char _pad1[1]; // padding to make sizeof(x_) multiple of 4 16. }struct_b; 17. 18. //struct_x强制 模1 对齐. 19. # pragma pack (1) 20. typedef struct 21. { 22. char a; // 1 byte 23. int b; // 4 bytes 24. short c; // 2 bytes 25. } struct_c; 26. # pragma pack () 27. 28. //struct_x被强制 模2 对齐. 29. # pragma pack (2) 30. typedef struct 31. { 32. char a; // 1 byte 33. int b; // 4 bytes 34. short c; // 2 bytes 35. } struct_d; 36. # pragma pack () 37. //struct_x被强制 模8对齐. 38. # pragma pack (8) 39. typedef struct 40. { 41. char a; // 1 byte 42. int b; // 4 bytes 43. short c; // 2 bytes 44. } struct_e; 45. # pragma pack () 46. typedef struct 47. { 48. char a; 49. short b; 50. char c; 51. }struct_f; 52. typedef struct 53. { 54. char a; 55. char c; 56. short b; 57. }struct_g; 58. typedef struct 59. { 60. char b; 61. int a; 62. short c; 63. }struct_h; 64. typedef struct{ 65. unsigned char incon: 8; /*incon占用低字节的0~7共8位*/ 66. unsigned char txcolor: 4;/*txcolor占用高字节的0~3位共4位*/ 67. unsigned char bgcolor: 3;/*bgcolor占用高字节的4~6位共3位*/ 68. unsigned char blink: 1; /*blink占用高字节的第7位*/ 69. }struct_i; 70. typedef struct{ //整个结构体要以2(sizeof(unsigned short))对齐 71. unsigned short incon: 8; 72. unsigned short txcolor: 4; 73. unsigned short bgcolor: 3; 74. unsigned short blink: 1; 75. }struct_j; 76. typedef struct{ //整个结构体要以4(sizeof(unsigned int))对齐 77. unsigned int incon: 8; 78. unsigned int txcolor: 4; 79. unsigned int bgcolor: 3; 80. unsigned int blink: 1; 81. }struct_k; 82. typedef struct{ //整个结构体要以4(sizeof(unsigned int))对齐 83. unsigned incon: 8; 84. unsigned txcolor: 4; 85. unsigned bgcolor: 3; 86. unsigned blink: 1; 87. }struct_l; 88. 89. class CLASS_A{ 90. char x; 91. void func(); 92. }; 93. class CLASS_B{ 94. char x; 95. virtual void func(); 96. }; 97. class CLASS_C{ 98. char x; 99. char y; 100. virtual void func(); 101. }; 102. class CLASS_D{ 103. char x; 104. char* z; 105. char y; 106. virtual void func(); 107. }; 108. class CLASS_E{ 109. char x; 110. CLASS_A a; 111. void func(); 112. }; 113. class CLASS_F{ 114. char x; 115. int y; 116. }; 117. class CLASS_G{ 118. char x; 119. CLASS_F f; 120. void func(); 121. }; 122. class CLASS_H{ 123. char x; 124. CLASS_F* f; 125. void func(); 126. }; 127. 128. class CLASS_I{ 129. char x; 130. virtual void func(); 131. virtual void func1(); 132. }; 133. 134. #define NAMEOF(x) #x 135. #define PRINT_SIZEOF(x) do{printf("sizeof(%s)=%dn",NAMEOF(x),sizeof(x));}while(0) 136. int main(){ 137. PRINT_SIZEOF(struct_a); 138. PRINT_SIZEOF(struct_b); 139. PRINT_SIZEOF(struct_c); 140. PRINT_SIZEOF(struct_d); 141. PRINT_SIZEOF(struct_e); 142. PRINT_SIZEOF(struct_f); 143. PRINT_SIZEOF(struct_g); 144. PRINT_SIZEOF(struct_h); 145. PRINT_SIZEOF(struct_i); 146. PRINT_SIZEOF(struct_j); 147. PRINT_SIZEOF(struct_k); 148. PRINT_SIZEOF(struct_l); 149. PRINT_SIZEOF(CLASS_A); 150. PRINT_SIZEOF(CLASS_B); 151. PRINT_SIZEOF(CLASS_C); 152. PRINT_SIZEOF(CLASS_D); 153. PRINT_SIZEOF(CLASS_E); 154. PRINT_SIZEOF(CLASS_F); 155. PRINT_SIZEOF(CLASS_G); 156. PRINT_SIZEOF(CLASS_H); 157. PRINT_SIZEOF(CLASS_I); 158. 159. return 0; 160. } 复制代码 六.例子结果 sizeof(struct_a)=12 sizeof(struct_b)=12 sizeof(struct_c)=7 sizeof(struct_d)=8 sizeof(struct_e)=12 sizeof(struct_f)=6 sizeof(struct_g)=4 sizeof(struct_h)=12 sizeof(struct_i)=2 sizeof(struct_j)=2 sizeof(struct_k)=4 sizeof(struct_l)=4 sizeof(CLASS_A)=1 sizeof(CLASS_B)=8 sizeof(CLASS_C)=8 sizeof(CLASS_D)=16 sizeof(CLASS_E)=2 sizeof(CLASS_F)=8 sizeof(CLASS_G)=12 sizeof(CLASS_H)=8 sizeof(CLASS_I)=8 七.显示class的对象布局 VC8 的隐含编译项/d1reportSingleClassLayout和/d1reportAllClassLayout可以看对象的布局。例如你需要看 CLASS_G的对象布局,就在编译参数里面加上/d1reportSingleClassLayoutCLASS_G。更加详细的介绍请参考下面的第3 个网址。下面是CLASS_G的对象布局: class CLASS_G size(12): +--- 0 | x | 4 | CLASS_F f +---
本文发布于:2024-02-08 20:07:12,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170739403268655.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |