C/C++ 類型內存占用詳解


最近做一些面試題目碰到了很多次考察C/C++類型內存占用的題目,主要考察隊C/C++的指針、類型等的熟悉程度。
本blog為了方面大家參考,總結了常見的類型內存占用的情況,能力所限,若有問題,請指出!

1. 基本類型

C/C++的基本類型包括int/long等等,這些基本類型在內存中的字節數一般是固定的(當然根據不同bit的系統有所調整),下表是基本類型的占用字節數。


PS: 1byte=8bit, byte通常寫成大寫B, bit一般寫為小寫b

下表單位均為Byte

System char short int long long long float dobule void *
(任意類型指針)
32bit 1 2 4 4 8 4 8 4
64bit 1 2 4 8 8 4 8 8

不信?我們以代碼說話:

#include <iostream>

using namespace std;

int main()
{

    cout<<"sizeof(char)="<<sizeof(char)<<endl;
    cout<<"sizeof(short)="<<sizeof(short)<<endl;
    cout<<"sizeof(int)="<<sizeof(int)<<endl;
    cout<<"sizeof(unsigned int)="<<sizeof(unsigned int)<<endl;
    cout<<"sizeof(long)="<<sizeof(long)<<endl;
    cout<<"sizeof(long long)="<<sizeof(long long)<<endl;
    cout<<"sizeof(float)="<<sizeof(float)<<endl;
    cout<<"sizeof(double)="<<sizeof(double)<<endl;

    //Poiter type
    cout<<endl;
    cout<<"sizeof(void *)="<<sizeof(void *)<<endl;
    cout<<"sizeof(char *)="<<sizeof(char *)<<endl;
    cout<<"sizeof(int *)="<<sizeof(int *)<<endl;
    cout<<"sizeof(float *)="<<sizeof(float *)<<endl;
    cout<<"sizeof(double *)="<<sizeof(double *)<<endl;

    return 0;
}

結果如下(我的系統是64bit的):
mark


可以看到,任意類型的指針的位數皆為8byte,即與系統位數保持一致,因此,判斷系統的位數我可以直接用sizeof(void *).

2. 復雜類型

編程時,有時我們會碰到如下情況:

  • int *p[n]
  • int (*p)[n]

以上兩者到底有什么區別呢?
下面我們先看一下代碼:

#include <iostream>

using namespace std;

struct A
{
    int a;
    int b;
    long c;
    long d;
};

int main()
{
    //Complex type
    cout<<endl;

    int p1[5] = {1,2,3,4,5};    //①.P1相關
    //int *p1 = &p1+1;      //It's warning in c but error in C++
    cout<<"sizeof(p1)="<<sizeof(p1)<<endl;
    cout<<"sizeof(&p1)="<<sizeof(&p1)<<endl;
    cout<<"*(p1+1) = "<<*(p1+1)<<endl;

    cout<<endl;
    int *p2[3];             //②.P2相關
    int a[10] = {0};
    int b[10] = {0};
    int c[10] = {0};
    p2[0] = a;
    p2[1] = b;
    p2[2] = c;

    cout<<"int *p2[3], sizeof(p2)="<<sizeof(p2)<<endl;
    cout<<"int *p2[3], sizeof(&p2)="<<sizeof(&p2)<<endl;

    cout<<endl;
    int (*p3)[4];       //③.P3相關
    int d[3][4]={0};
    p3=d;
    cout<<"sizeof(p3)="<<sizeof(p3)<<endl;
    cout<<"sizeof(p3[0])="<<sizeof(p3[0])<<endl;

    return 0;
}

①. P1 : 數組

  • p1為一維數組的數組名,包含五個int型元素
  • p1雖然數值上與&p1[0]相等,但是兩者不是一個東西(&p1[0]是第一個元素的地址)
  • p1+1 其實就是 &p1[1]

②. P2 : 數組指針 (int *p2[n])

[]的優先級高於*,因此先p2[3],即p2先是一個數組,然后與*結合,*p2[n]即成了數組指針(數組內部存放的內容皆為指針)
mark

③. P3 : 指針數組(int(*p3)[n])

int (p3)[n],由於()優先級大於[],所以先(p3),后為數組,即p3所指向的對象是有n個int型元素的數組,即p3是指向一維數組的指針;p3的值即為該一維數組的地址。


以上,相當於二維數組。

mark

結果:

根據以上講解,你是否已經得出答案?
mark

3. 函數相關

有的時候,我們還會遇到以下的情況:

  • int *p() : 返回指針的函數
  • int (*p)() : 指向函數的指針

①. int *p() 指針函數

實際上,它就是一個函數,只不過返回類型為指針而已,和普通函數沒什么區別的。

#include <iostream>
#include <cstring>

using namespace std;

char *array()
{
    auto ptr = new char(10);
    ptr="hello";
    return ptr;
}

int main()
{
    char *pt = array();
    cout<<pt<<endl;

    return 0;
}

②. int (*p)(): 函數指針

指向函數的指針變量,其本質是一個指針。

int (*fptr)(int x); /*聲明一個函數指針*/
fptr = func; /*將func函數的首地址賦給該指針*/

每個函數都有一個入口地址,將該入口地址賦值給一個指針,通過該指針即可以調用這個函數。

#include <stdio.h>

void (*ptr)(char *str);

void prt1(char *str)
{
    printf("ptr1 string=%s\n", str);
    return ;
}

void prt2(char *str)
{
    printf("ptr2 string=%s\n", str);
    return ;
}

int main()
{
    ptr = prt1;
    (*ptr)("hello");

    ptr = prt2;
    (*ptr)("world");

    return 0;
}

特別類似於C++的多態是不是?

③. 函數的形式參數

有時候,我們函數的形參需要為指針,這個時候,其實我們可以有多重寫法

  • void *func(char *str)
  • void *func(char str[])
  • void *func(char str[n])

以上三種寫法一個意思,即使n小於實參的字節數也無妨,此處的n只是一個提示作用而已。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM