1. 程式人生 > >c/c++面試12-18------關與sizeof那些事兒

c/c++面試12-18------關與sizeof那些事兒

none 不同 都是 include png com rtu 偏移量 ecc

12 使用sizeof計算普通變量所占空間大小

(1)不同數據類型所占字節數不同(32位 64位系統不同)

int----->4

double----->4

char------->1

(2)代碼

技術分享圖片
 1 #include<stdio.h>
 2 #include <stdlib.h>
 3 void Func(char str[100])
 4 {
 5     printf("sizeof(str)=%d\n", sizeof(str));
 6 }
 7 int main()
 8 {
 9     char str[] = "hello";
10     char
*p = str; 11 int n = 10; 12 printf("sizeof(str)=%d\n", sizeof(str));//6 13 printf("sizeof(p)=%d\n", sizeof(p));//4 14 printf("sizeof(n)=%d\n", sizeof(n)); //4 15 void *pp = malloc(100); 16 printf("sizeof(pp)=%d\n", 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*,而且必須以‘\0‘結尾

  (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

好了 加油加油!!!

c/c++面試12-18------關與sizeof那些事兒