http://www.jb51.net/article/54980.htm
2014
指針是C語言的精髓,本文就以實(shí)例的形式詳細(xì)分析了C語言的長度和類型。對于初學(xué)者深入理解C語言程序設(shè)計(jì)有很好的參考價(jià)值。具體分析如下:
一般來說,如果考慮應(yīng)用程序的兼容性和可移植性,指針的長度就是一個問題,在大部分現(xiàn)代平臺上,數(shù)據(jù)指針的長度通常是一樣的,與指針類型無關(guān),盡管C標(biāo)準(zhǔn)沒有規(guī)定所有類型指針的長度相同,但是通常實(shí)際情況就是這樣。但是函數(shù)指針長度可能與數(shù)據(jù)指針的長度不同。
指針的長度取決于使用的機(jī)器和編譯器,例如:在現(xiàn)代windows上,指針是32位或是64位長
測試代碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<stdio.h> #include<math.h> #include<stdlib.h> #include<stddef.h> struct p{ int n; float f; }; int main() { struct p *sptr; printf ( "sizeof *char: %d\n" , sizeof ( char *)); printf ( "sizeof *int: %d\n" , sizeof ( int *)); printf ( "sizeof *float: %d\n" , sizeof ( float *)); printf ( "sizeof *double: %d\n" , sizeof ( double *)); printf ( "sizeof *struct: %d\n" , sizeof (sptr)); return 0; } |
運(yùn)行結(jié)果如下圖所示:
指針相關(guān)的預(yù)定義類型:
① size_t:用于安全地表示長度
② ptrdiff_t:用于處理指針?biāo)阈g(shù)運(yùn)算
③ intptr_t:用于存儲指針地址
④ uintptr_t:用于存儲指針地址
分述如下:
一、size_t類型
size_t 類型是標(biāo)準(zhǔn)C庫中定義的,應(yīng)為unsigned int,在64位系統(tǒng)中為 long unsigned int。 C語言中,此類型位于頭文件stddef.h中。它是一個與機(jī)器相關(guān)的unsigned類型,其大小足以保證存儲內(nèi)存中對象的大小,它的目的是提供一種可移植的方法來聲明與系統(tǒng)中可尋址的內(nèi)存區(qū)域一致的長度:
因?yàn)镃/C++標(biāo)準(zhǔn)只定義一最低的位數(shù),而不是必需的固定位數(shù)。而且在內(nèi)存里,對數(shù)的高位對齊存儲還是低位對齊存儲各系統(tǒng)都不一樣。為了提高代碼的可移植性,就有必要定義這樣的數(shù)據(jù)類型。一般這種類型都會定義到它具體占幾位內(nèi)存等。當(dāng)然,有些是編譯器或系統(tǒng)已經(jīng)給定義好的。經(jīng)測試發(fā)現(xiàn),在32位系統(tǒng)中size_t是4字節(jié)的,而在64位系統(tǒng)中,size_t是8字節(jié)的,這樣利用該類型可以增強(qiáng)程序的可移植性。
size_t類型用作sizeof操作符的返回類型,同時(shí)也是很多函數(shù)的參數(shù)類型,包括malloc和strlen
在聲明例如字符數(shù)、或者數(shù)組索引這樣的長度變量時(shí)用size_t是好的做法,它經(jīng)常用于循環(huán)計(jì)數(shù)器、數(shù)組索引,有時(shí)候還用在指針?biāo)阈g(shù)運(yùn)算上
打印size_t類型的值要小心,這是無符號值,如果選錯格式說明符,可能會得到不可靠的結(jié)果,推薦的格式說明符是%zu,在某些情況下可以考慮用%u或%lu替代
二、ptrdiff_t類型
ptrdiff_t是C99標(biāo)準(zhǔn)庫中定義的一個與機(jī)器相關(guān)的數(shù)據(jù)類型,定義在stddef.h這個文件內(nèi)。ptrdiff_t類型變量通常用來保存兩個指針減法操作的結(jié)果。
ptrdiff_t通常被定義為long int類型,size_t 是unsigned 類型,而 ptrdiff_t 則是 signed 整型。
這兩種類型的差別體現(xiàn)了它們各自的用途:size_t 類型用于指明數(shù)組長度,它必須是一個正數(shù);ptrdiff_t 類型則應(yīng)保證足以存放同一數(shù)組中兩個指針之間的差距,它有可能是負(fù)數(shù)。
三、intptr_t與uintptr_t類型
intptr_t與uintptr_t類型用來存放指針地址,它們提供了一種可移植且安全的方法聲明指針,而且與系統(tǒng)中使用的指針的長度相同,對于把指針轉(zhuǎn)化為整數(shù)形式很有用。uintptr_t是intptr_t的無符號版本
關(guān)于intptr_t的類型定義如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /* Types for `void *' pointers. */ #if __WORDSIZE == 64 # ifndef __intptr_t_defined typedef long int intptr_t ; # define __intptr_t_defined # endif typedef unsigned long int uintptr_t ; #else # ifndef __intptr_t_defined typedef int intptr_t ; # define __intptr_t_defined # endif typedef unsigned int uintptr_t ; #endif |
從定義可以看出,intptr_t在不同的平臺是不一樣的,始終與地址位數(shù)相同,因此用來存放地址。
概念上, 盡管地址是指針, 內(nèi)存管理常常使用一個無符號的整數(shù)類型更好地完成; 內(nèi)核對待物理內(nèi)存如同一個大數(shù)組, 并且內(nèi)存地址只是一個數(shù)組索引. 進(jìn)一步地, 一個指針容易解引用; 當(dāng)直接處理內(nèi)存存取時(shí), 你幾乎從不想以這種方式解引用. 使用一個整數(shù)類型避免了這種解引用, 因此避免了 bug. 因此, 內(nèi)核中通常的內(nèi)存地址常常是 unsigned long, 利用了指針和長整型一直是相同大小的這個事實(shí), 至少在 Linux 目前支持的所有平臺上.C99 標(biāo)準(zhǔn)定義了 intptr_t 和 uintptr_t 類型給一個可以持有一個指針值的整型變量
測試代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <stdint.h> #include <string.h> #include <assert.h> #define ID_STR_LEN 12 #define NAME_STR_LEN 10 typedef struct student { char id[ID_STR_LEN]; char name[NAME_STR_LEN]; uint8_t age; }student; student * create_student() { student *stu = (student *) malloc ( sizeof (student)); if (stu == NULL) return NULL; memset (stu, 0, sizeof (student)); return stu; } void *free_student(student *stu) { if (stu) free (stu); return 0; } static void init_student(student * stu) { assert (stu); const char *id = "2013112210" ; const char *name = "Anker" ; uint8_t age = 21; memcpy (stu->id, id, strlen (id)); memcpy (stu->name, name, strlen (name)); stu->age = age; } static int handle_student( intptr_t handle) { if (handle == 0) { return -1; } student *stu = (student*)handle; printf ( "id: %s\n" , stu->id); printf ( "name: %s\n" , stu->name); printf ( "age: %u\n" , stu->age); return 0; } int main( void ) { student *stu; stu = create_student(); init_student(stu); //將指針轉(zhuǎn)換為intptr_t類型 intptr_t handle = ( intptr_t )stu; handle_student(handle); free_student(stu); return 0; } |
希望本文所述實(shí)例對大家C程序設(shè)計(jì)的學(xué)習(xí)有所幫助。