32位機和64位機程式設計時應該注意些什麼

2021-07-04 22:39:23 字數 1167 閱讀 3543

1.資料型別在各種機器中所佔的位元組數不同

16位平台

char         1個位元組8位

short        2個位元組16位

int            2個位元組16位

long         4個位元組32位

指標         2個位元組

32位平台

char         1個位元組8位

short        2個位元組16位

int            4個位元組32位

long         4個位元組

long long 8個位元組

指標         4個位元組

64位平台

char         1個位元組

short        2個位元組

int            4個位元組

long         8個位元組(區別)

long long 8個位元組

指標        8個位元組(區別)

2.為了保證平台的通用性,程式中盡量不要使用long資料庫型

3.程式設計中要盡量使用sizeof來計算資料型別的大小,如:int *p = (int *)malloc(sizeof(int)*100);而不要int *p = (int *)malloc(400);

4.使用int時也可以使用intptr_t來保證平台的通用性,它在不同的平台上編譯時長度不同,但都是標準的平台長度,比如64位機器它的長度就是8位元組,32位機器它的長度是4位元組,定義如下: #if __wordsize == 64

typedef long int                intptr_t;

#else

typedef int                        intptr_t;

#endif

5.ssize_t和size_t分別是unsigned和signed size of computer word size。它們也是表示計算機的字長,在32位機器上是int型,在64位機器上long型,從某種意義上來說它們等同於intptr_t和 uintptr_t。它們在stddef.h裡面定義。需要注意的是socket的accept函式在有些作業系統上使用size_t是不正確的,因為 accept接收的int*型別,而size_t可能是long int 型別。後來bsd使用sock_t來替代它

32位機和64位機下面各型別sizeof的大小

執行結果如下 分別用藍色和紅色標記了兩者的差異 機器平台 x86 64 處理器 作業系統 red hat 4.1.2 14 編譯器 gcc version 4.1.2 20070626 size of char is 1 size of unsigned char is 1 size of sign...

32位機和64位機下面各型別sizeof的大小

機器平台 x86 64 處理器 作業系統 red hat 4.1.2 14 編譯器 gcc version 4.1.2 20070626 size of char is 1 size of unsigned char is 1 size of signed char is 1 size of int...

32位機和64位機下面各型別sizeof的大小

這種型別的題目出的太多了,還是整理下吧!一 機器平台 x86 64 處理器 作業系統 red hat 4.1.2 14 編譯器 gcc version 4.1.2 20070626 char 1 unsigned char 1 signed char 1 int 4 short 2 long 8 l...