1. 程式人生 > >[筆試題]sizeof系列面試題中的易錯之處

[筆試題]sizeof系列面試題中的易錯之處

-- png return ascii stream pointer .com func 指針的指針

sizeof系列筆試題看似簡單,其實如果不深入探究,很容易出錯,本人就有時敗在了這,特寫篇博客總結一番,引以為戒。

V1.0 32位和64位編譯器的區別

測試代碼如下:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    //32和64位編譯器區別: 除了*與long隨操作系統子長變化而變化外,其他的都固定不變(32位和64相比)
    //32: sizeof(*)=4 sizeof(long)=4
    //64: sizeof(*)=8 sizeof(long)=8
    //這裏為32位編譯器
    cout << "bool size = " << sizeof(bool) << endl; //1
    cout << "char size = " << sizeof(char) << endl; //1
    //--
    cout << "wchar_t size = " << sizeof(wchar_t) << endl; //=unsigned short 2
    cout << "short size = " << sizeof(short) << endl; // 2 short=2!=int
    cout << "int size = " << sizeof(int) << endl;   //4
    cout << "long size = " << sizeof(long) << endl; //4    [long=int=4 為32位編譯器]
    cout << "long long size = " << sizeof(long long) << endl;   //8
    cout << "char*=" << sizeof(char*) << " int*=" << sizeof(int*) << endl; // 4 [*=4 為32位編譯器]
    //--
    cout << "float size = " << sizeof(float) << endl; //4
    cout << "double size = " << sizeof(double) << endl; //8
    cout << "long double size = " << sizeof(long double) << endl;   //8 【註意】

    return 0;
}

執行結果如下:
技術分享圖片

V2.0 sizeof字符數組

測試代碼如下:

#include "stdafx.h"
#include <iostream>
using namespace std;

void printSize(char aInFunc[])
{
    printf("sizeof(aInFunc) = %lu\n",sizeof(aInFunc));
}

int main()
{
    char str[5] = {0};
    char str1[] = "hello";
    char str2[5] = {‘0‘};
    char str3[5] = {‘0‘,‘0‘,‘0‘};

    //str為含有5個元素的數組(後面會自動填充空格‘ ‘),數組名代表首元素的地址,所以sizeof(str)代表整個數組所占的內存空間
    //容易誤認為傳的是地址,判斷為4,但實際上你傳的是數組名,數組名不等價於地址
    printf("sizeof(str) = %lu\n",sizeof(str));  //  5
    printf("sizeof(str2) = %lu\n",sizeof(str));  //  5
    printf("sizeof(str3) = %lu\n",sizeof(str3));  //  5
    //*str為首元素地址的內容,即是首元素的值
    printf("sizeof(*str) = %lu\n",sizeof(*str));  //  1
    //容易認為是5,但"hello"是字符串,最後一個是‘\0‘
    printf("sizeof(hello) = %lu\n",sizeof("hello"));  //  6
    //str1是數組,保存的是字符串,所以長度為6
    printf("sizeof(str1) = %lu\n",sizeof(str1));  //  6
    //在數組作為參數傳入的時候實際上就退化為普通的指針了,不過這個指針實際上類似於const pointer
    printSize(str); // 4

    return 0;
}

執行結果如下:
技術分享圖片

V3.0 strlen字符數組

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    char str[5] = {0};
    char str1[] = "hello";
    char str2[5] = {‘0‘};
    char str3[5] = {‘0‘,‘0‘,‘0‘};

    //strlen函數 需要註意的是,這個函數返回的結果不包含\0
    //ASCII碼中: 十進制的0 - 字符空格‘ ‘ - 轉義字符‘\0‘ - 代碼NULL ,所以strlen(str) == 0
    printf("strlen(str) = %lu\n",strlen(str));  //  0  【特別註意】
    printf("strlen(str1) = %lu\n",strlen(str1));  //  5  【特別註意】
    printf("strlen(str2) = %lu\n",strlen(str2));  //  1  【特別註意】  字符‘0‘,對應ASCII碼為48
    printf("strlen(str3) = %lu\n",strlen(str3));  //  3  【特別註意】
    printf("strlen(hello) = %lu\n",strlen("hello"));  //  5

    return 0;
}

執行結果如下:
技術分享圖片

V4.0 sizeof數組指針/指針數組

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    //VS為32位編譯器
    char **str1[4]; //str是指針數組,數組包含4個指向指針的指針(**)
    char *(*str2)[4]; //str是數組指針,指向包含4個指針的char*數組
    char (**str3)[4]; //str是數組指針,指向包含4個char的char數組

    printf("strlen(str) = %lu\n",sizeof(str1));  //  4*4 = 16 
    printf("strlen(str1) = %lu\n",sizeof(str2));  //  4
    printf("strlen(str2) = %lu\n",sizeof(str3));  //  4

    return 0;
}

執行結果如下:
技術分享圖片

[筆試題]sizeof系列面試題中的易錯之處