C/C++ 面试-内存对齐 即不同数据类型存储空间

下面列举了Dev-C++下基本类型所占位数和取值范围:

        基本型                          所占位数              取值范围                输入符举例                                 输出符举例

    -- -- char                            8                        -2^7 ~ 2^7-1              %c                                         %c、%d、%u
    
    signed -- char                    8                         -2^7 ~ 2^7-1              %c                                           %c、%d、%u
    
    unsigned -- char                8                          0 ~ 2^8-1                  %c                                            %c、%d、%u
    
    [signed] short [int]            16                            -2^15 ~ 2^15-1        %hd                                              %hd
    
    unsigned short [int]           16                            0 ~ 2^16-1           %hu                                  %hu、%ho、%hx
    
    [signed] -- int                    32                        -2^31 ~ 2^31-1         %d
    
    unsigned -- [int]                 32                        0 ~ 2^32-1              %u、%o、%x
    
    [signed] long [int]              32                       -2^31 ~ 2^31-1         %ld
    
    unsigned long [int]             32                         0 ~ 2^32-1               %lu、%lo、%lx
    
    [signed] long long [int]       64                        -2^63 ~ 2^63-1              %I64d
    
    unsigned long long [int]      64                    0 ~ 2^64-1                 %I64u、%I64o、%I64x
    
    -- -- float                            32                    +/- 3.40282e+038              %f、%e、%g
    
    -- -- double                        64                +/- 1.79769e+308                   %lf、%le、%lg %f、%e、%g
    
    -- long double                    96                   +/- 1.79769e+308          %Lf、%Le、%Lg

注意:int型数据  在不同位数的系统中   所占位数不一样   一些编译器不同也有影响   

16         2*8位 

32         4*8位

64         8*8位

1 BYTE=8 BIT

内存对齐可以用一句话来概括:

“数据项只能存储在地址是数据项大小的整数倍的内存位置上”

例如int类型占用4个字节,地址只能在0,4,8等位置上。

      double类型占用8个字节,地址只能在0,8,16等位置上面

如果代码之中有特别规定的话     对齐方式以程序为主

#pragma pack(n)

如果在代码之中出现了上面的语句

那么对齐机制   为地址只能在0,n,2*n,3*n等位置上面

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <pthread.h>
 4 #include <windows.h>
 5 using namespace std;
 6 struct X1
 7 {
 8     int i;//4个字节
 9     char c1;//1个字节
10     char c2;//1个字节
11     long a;//4个字节
12     float b;
13 } x1;
14 struct X2
15 {
16     char c1;//1个字节
17     int i;//4个字节
18     char c2;//1个字节
19     long a;//4个字节
20     float b;
21 } x2;
22 struct X3
23 {
24     char c1;//1个字节
25     char c2;//1个字节
26     int i;//4个字节
27     long a;//4个字节
28     float b;
29 } x3;
30 struct X4
31 {
32     int i;//4个字节
33     char c1;//1个字节
34     char c2;//1个字节
35     long a;//4个字节
36     float b;
37 } x4;
38 int main()
39 {
40     cout<<"long "<<sizeof(long)<<"
";
41     cout<<"float "<<sizeof(float)<<"
";
42     cout<<"int "<<sizeof(int)<<"
";
43     cout<<"char "<<sizeof(char)<<"
";
44     cout<<"x1 的大小 "<<sizeof(x1)<<"
";
45     cout<<"x2 的大小 "<<sizeof(x2)<<"
";
46     cout<<"x3 的大小 "<<sizeof(x3)<<"
";
47     cout<<"x4 的大小 "<<sizeof(x4)<<"
";
48     return 0;
49 }
50 运行结果:
51 long 4
52 float 4
53 int 4
54 char 1
55 x1 的大小 16
56 x2 的大小 20
57 x3 的大小 16
58 x4 的大小 16
View Code

三个结构体   结构体中的数据都是一样的  但是结构体所占的空间不一样   这就是对齐机制对于存储的影响

原文地址:https://www.cnblogs.com/52why/p/7634700.html