c/c++面试12-18------关与sizeof那些事儿

12 使用sizeof计算普通变量所占空间大小

(1)不同数据类型所占字节数不同(32位 64位系统不同)

int----->4

double----->8

char------->1

(2)代码

 1 #include<stdio.h>
 2 #include <stdlib.h>
 3 void Func(char str[100])
 4 {
 5     printf("sizeof(str)=%d
", sizeof(str));
 6 }
 7 int main()
 8 {
 9     char str[] = "hello";
10     char *p = str;
11     int n = 10;
12     printf("sizeof(str)=%d
", sizeof(str));//6
13     printf("sizeof(p)=%d
", sizeof(p));//4
14     printf("sizeof(n)=%d
", sizeof(n)); //4
15     void *pp = malloc(100);
16     printf("sizeof(pp)=%d
", sizeof(pp));//4
17     Func(str);
18     getchar();
19 }
View Code

注意:

  (1)数组作为实际参数传给函数的时候,当作指针来看待,不是绝对的。

  (2)sizeof不是函数  是一个运算符

13 使用sizeof计算类对象所占空间大小

(1)代码

 1 #include<stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 using namespace std;
 5 class A
 6 {
 7 public:
 8     int i;
 9 };
10 class B
11 {
12 public:
13     char ch;
14 };
15 class C
16 {
17 public:
18     int i;
19     short j;
20 };
21 
22 class D
23 {
24 public:
25     int i;
26     short j;
27     char ch;
28 };
29 class E
30 {
31 public:
32     int i;
33     int ii;
34     short j;
35     char ch;
36     char chr;
37 };
38 
39 class F
40 {
41 public:
42     int i;
43     int ii;
44     int iii;
45     short j;
46     char ch;
47     char chr;
48 };
49 
50 int main()
51 {
52     cout << "sizeof(int)=" << sizeof(int) << endl;
53     cout << "sizeof(short)=" << sizeof(short) << endl;
54 
55     cout << "sizeof(char)=" << sizeof(char) << endl;
56 
57     cout << "sizeof(A)=" << sizeof(A) << endl;
58 
59     cout << "sizeof(B)=" << sizeof(B) << endl;
60 
61     cout << "sizeof(C)=" << sizeof(C) << endl;
62 
63     cout << "sizeof(D)=" << sizeof(D) << endl;
64 
65     cout << "sizeof(E)=" << sizeof(E) << endl;
66     cout << "sizeof(F)=" << sizeof(F) << endl;
67 
68     getchar();
69     return 0;
70 }
View Code

(2)吃惊的代码结果

(3)分析分析

  a:32位win操作系统 char-->1个字节 int---->4个字节 short----->2字节

  sizeof(A)=sizeof(int)=4

  sizeof(B)=sizeof(char)=1

  sizeof(C)=sizeof(int)+sizeof(short)=4+2=6

  sizeof(D)=sizeof(int)+sizeof(short)+sizeof(char)=7

  sizeof(E)=2*sizeof(int)+2*sizeof(char)+sizeof(short)=12

  sizeof(F)=3+sizeof(int)+2*sizeof(char)+sizeof(short)=15

------------>这和运行的结果有出入呀

  (b)这就是字节对齐造成的,结论如下;

------------>结构体每个成员相当于结构体的首地址偏移量都是成员大小的整数倍

------------>结构体总大小为结构体最宽基本类型成员大小的整数倍

举个例子:

struct A

{

  char c1;

  int i;

  char c2;

};--------------------->c1的偏移量是0,i的偏移量是4,那么c1和i之间就有3个字节的填充,再加上i的四个字节,所以c2的偏移量就是8,那么总共1+3+4+1=9;但是这个结构体最宽的基本类型是int,为4字节,要是它整数,那么就需要填充3字节 9+3=12,所以为12字节。也不知道清楚了没!!

15 使用sizeof计算含有虚函数类对象的空间大小

(1)代码

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Base
 6 {
 7 public:
 8     Base(int x) :a(x)
 9     {
10 
11     }
12     void print()
13     {
14         cout << "base" << endl;
15     }
16 private:
17     int a;
18 };
19 class Drived :public Base
20 {
21 public:
22     Drived(int x) :Base(x - 1), b(x)
23     {
24 
25     }
26     void print()
27     {
28         cout << "derived" << endl;
29     }
30 private:
31     int b;
32 };
33 class A
34 {
35 public:
36     A(int x) :a(x)
37     {
38 
39     }
40     virtual void print()
41     {
42         cout << "A" << endl;
43     }
44 private:
45     int a;
46 };
47 class B :public A
48 {
49 public:
50     B(int x):A(x - 1),b(x)
51     {
52 
53     }
54     virtual void print()
55     {
56         cout << "b" << endl;
57     }
58 public:
59     int b;
60 };
61 
62 int main()
63 {
64     Base obj1(1);
65     cout << "size of base obj is" << sizeof(obj1) << endl;
66     Drived obj2(2);
67     cout << "size of base obj is" << sizeof(obj2) << endl;
68 
69     A a(1);
70     cout << "size of A obj is" << sizeof(a) << endl;
71     B b(2);
72     cout << "size of B obj is" << sizeof(B) << endl;
73     getchar();
74     return 0;
75 }
View Code

(2)解析

  a;A是空类,编译器会安插一个char给空雷用来标记它的每一个对象

  b:类D是虚继承A,编译器会为该类一个指向父类的指针,4字节

  c:类E虚继承AB  8

16 sizeof和strlen哪些区别

  (1)sizeof是操作符 strlen是函数

  (2)sizeof可以用类型做参数,strlen参数必须是char*,而且必须以''结尾

  (3)在计算 字符串数组长度有区别

    a:char str[20]="0123456789"

      int a = strlen(str);//以ox00结束的字符串长度 10

      int b = sizeof(str)//分配str[20]所占内存空间的大小 20

17 sizeof有哪些用途

  (1) 与分配和IO系统那样的例程进行通信

    void* malloc(size_t size)

    size_t fread(void* ptr,size_t size,size_t nmemb,FILE* stream)

        (2)动态分配对象 让系统知道分配多大的内存

  (3)如果操作数是函数中的数组形参或者函数类型的形参,那么sizeof给出的是指针大小

18 使用sizeof计算联合体的大小

(1)注意

  a:联合体的大小取决于它所有成员中占用空间大小最大的成员的大小

(2)代码

 1 #include <iostream>
 2 using namespace std;
 3 union u
 4 {
 5     double a;
 6     int b;
 7 };
 8 union u2
 9 {
10     char a[13];
11     int b;
12 };
13 union u3
14 {
15     char a[13];
16     char b;
17 };
18 int main()
19 {
20     cout << sizeof(u) << endl;//8
21     cout << sizeof(u2) << endl;//16
22     cout << sizeof(u3) << endl;//13
23     getchar();
24 }
View Code

(3)分析

  a:对于u,大小最大为double 所以sizeof(u)=sizeof(double)=8

  b:对于u2,最大是char buf[13],但是还有int,4字节,所以对齐方式变为4,所以占用空间变为最接近13的16

  c:对于u3,最大的空间是char[13]数组,所以13

---------->其实对齐的方案是可以更改的,对于c++固有对齐取编译器对齐方式与自身大小较小的一个,看下面的例子

  d(代码2):

  

 1 #include <iostream>
 2 #pragma pack(2)
 3 
 4 union u
 5 {
 6     char buf[9];
 7     int a;            
 8 };
 9 int main()
10 {
11      cout<<sizeof(u)<<endl;//10 因为对齐从4变化为2 9最近且满足2倍数10
12      return 0;  
13 }
View Code

好了  加油加油!!!

原文地址:https://www.cnblogs.com/lanjianhappy/p/7920523.html