關於sizeof,我首先想說的是它是一個宏,不是一個函數,其次sizeof操作符的結果類型是size_t,它在頭文件中typedef爲unsigned int類型,該類型保證能容納實現所建立的最大對象的字節大小,sizeof用來計算數據類型所佔的內存大小。
先貼一些常見的sizeof求值對象:
// sizeof.cpp : 定義控制檯應用程序的入口點。
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
/* 整形結構體 */
struct TestShort
{
/* a佔四個字節 */
int a;
/* c佔1一個字節 */
char c;
/* short int 佔2個字節 */
short int b;
};
struct TestShort1
{
/* c佔1一個字節 */
char c;
/* a佔四個字節 */
int a;
/* short int 佔2個字節 */
short int b;
};
struct Test
{
/* a佔四個字節 */
int a;
/* c佔1一個字節 */
char c;
};
/* 柔性結構體 */
struct TestSoft
{
/* a佔四個字節 */
int a;
/* c佔1一個字節 */
char c;
/* 定義了大小不確定的數組b,所以是柔性結構體 */
int b[];
};
/* long類型的結構體 */
struct TestDouble
{
/* a佔八個個字節 */
double a;
/* c佔1一個字節 */
char c;
};
int main(int argc, char *argv[])
{
int data = 0;
int *pInt = &data;
int intArray[5] = {0};
int doubleIntArray[3][5];
char charArray[10];
char pStr[] = "hello,world";
char *pChar = "hello,world";
char pOx[] = "\023cd";
char pUnox[] = "\03834";
char pUnox1[] = "\0834";
/* int類型佔4個字節,所以結果爲4 */
printf( "sizeof(data) = %d\n", sizeof(data) );
/* int類型的指針,求的是指針所佔的內存大小, 所以爲一個int型數據大小即4 */
printf( "sizeof(pInt) = %d\n", sizeof(pInt) );
/* intArray爲一個數組名,所以結果爲數組的大小,即5*4=20 */
printf( "sizeof(intArray) = %d\n", sizeof(intArray) );
/* doubleIntArray爲二維數組,結果爲二維數組的大小即3*5*4=60 */
printf( "sizeof(doubleIntArray) = %d\n", sizeof(doubleIntArray) );
/* 字符數組,結果爲數組大小即10*1=1 */
printf( "sizeof(charArray) = %d\n", sizeof(charArray) );
/* 通過賦值確定大小的字符數組,結果依然爲數組大小即12*1=12 */
printf( "sizeof(pStr) = %d\n", sizeof(pStr) );
/* pChar爲一個指向字符串的指針,所以結果爲指針大小即4 */
printf( "sizeof(pChar) = %d\n", sizeof(pChar) );
/* pOx爲含有轉義字符的字符串,\023爲一個字符,結果爲數組大小即4*1=4 */
printf( "sizeof(pOx) = %d\n", sizeof(pOx) );
/* pUnox同pOx一樣,第一個字符爲\03,由於8不屬於八進制,所以結果爲5*1=5 */
printf( "sizeof(pUnox) = %d\n", sizeof(pUnox) );
/* pUnox1同pUnox,不過第一個字符爲\0,所以結果爲5*1=6 */
printf( "sizeof(pUnox1) = %d\n", sizeof(pUnox1) );
/* 由於第一個字符爲\0,所以字符串長度爲0,所以結果爲0 */
printf( "strlen(pUnox1) = %d\n", strlen(pUnox1) );
/* double佔八個字節 */
printf( "sizeof(double) = %d\n", sizeof(double) );
/* 結構體的大小要考慮按照結構體內最大數據類型大小對齊 */
/* 一個int佔4個字節,一個char只需佔一個字節就夠了,但是要考慮
* 對齊,且本結構體內最大的數據類型爲int型(佔4個字節),而c結構
* 體內無其他數據了,所以char由於要對其,所以要給它分配四個字
* 節,所以結構體的大小爲4+4=8
*/
printf( "sizeof(struct Test) = %d\n", sizeof(struct Test) );
/* 同Test分析,但是由於後面的c和b加起來只需要分配3個字節,通過
* 對齊後,可以分配在一個四字節的內存內,所以結果還是8 */
printf( "sizeof(struct TestShort) = %d\n", sizeof(struct TestShort) );
/* 由於只需一個字節,而接下來的a佔四個字節,由於每一種數據類型的地址都
* 要求能被數據類型大小整除,所以a需要佔四個字節,所以結果爲4+4+4=12 */
printf( "sizeof(struct TestShort1) = %d\n", sizeof(struct TestShort1) );
/* 由於結構體柔性結構體,最後面的數組不佔內存所以結果和Test一樣,即8 */
printf( "sizeof(struct TestSoft) = %d\n", sizeof(struct TestSoft) );
/* 此結構體的最大數據類型爲8個字節的,所以本結構體的對齊是按照8字節對
* 齊的,所以本結構體的大小是8+8=16 */
printf( "sizeof(struct TestLong) = %d\n", sizeof(struct TestDouble) );
getchar();
return 0;
}
運行結果爲:
總結:sizeof求的是數據類型在內存中需要佔有的內存大小(單位爲字節),需要注意的幾點如下
- 基本數據類型大小要記住;
- 要知道sizeof的求值對象,是一個普通變量還是指針,或者是數組;
- 對於指針而言,大小即爲指針大小,不管是什麼指針;
- 對於數組而言,sizeof的結果就是數組的大小;
- 對於求字符串的長度要注意轉義字符,清楚什麼時候是轉義字符的結束;
- 求結構體(類)大小的時候要注意1)考慮對齊 2)對齊方式是以結構體最大的數據類型對齊的 3)每一種數據類型的地址都可以被其數據類型的大小整除;
- c和c++的sizeof('0')的結果是不一樣的,c中會把'0'當成ini類型,而c++中則會把'0'當作char類型,所以纔會有差異。