就像我在http://www.cnblogs.com/wuyuegb2312/p/3219659.html 文章中評論的那樣,我也碰到了被提問這個malloc(0)的返回值問題,雖然感覺這樣做在實際中沒有任何意義,但既然被提問到了,那總得給點答復。當時的回答是“返回一個NULL指針”。
就像@五岳查看man結果的一樣,我也查看了,malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().這句話翻譯起來,就是傳個0的話,返回值要么是NULL,要么是一個可以被free調用的唯一的指針。那是不是這篇文章中說的,通過這句話“
if(int pp = (strlen(ptr=(char *)malloc(0))) == 0)
”來判斷是不是NULL指針呢?當然,實際情況到底如何,還得看代碼。
剛看到@garbageMan一篇文章 http://www.cnblogs.com/pmer/p/3222648.html 這樣寫道:“malloc(0)唯一不同的地方就是,就算你申請內存成功,即malloc(0)返回值不為NULL,你也沒法使用這塊內存。”那到底是不是就沒法使用呢?
我的測試代碼:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <malloc.h> int alloc_memory(char *p , int size) { printf("\nbefore malloc %p\n",p); p = (char *)malloc(size); if(!p) { printf("malloc error \n"); return -1; } //len of malloc(0) printf("len of malloc(%d) is %d ,the ture is %d\n",size,strlen(p),malloc_usable_size(p)); //the first member printf("the first member of malloc(%d) is %p:%d \n",size,p,*p); //set the first member *p = 10; printf("set the first member of malloc(%d) is %p:%d \n",size,p,*p); //memcpy memset(p,'\0',12); memcpy(p,"01234567890123456789",12); printf("after memcpy , the content is %s len is %d , the ture is %d \n",p,strlen(p),malloc_usable_size(p)); free(p); p = NULL; printf("\n"); } int main(int argc ,char **argv) { int size = -1; char *p = NULL; //malloc(0) size = 0; alloc_memory(p,size); //malloc(5) size = 5; alloc_memory(p,size); //malloc(20) size = 20; alloc_memory(p,size); return 0; }
測試結果如下:
tiger@ubuntu:/mnt/hgfs/e/Lessons/MyExercise/UtilLibs/EXERCISE$ gcc -o malloc malloc.c tiger@ubuntu:/mnt/hgfs/e/Lessons/MyExercise/UtilLibs/EXERCISE$ ./malloc before malloc (nil) len of malloc(0) is 0 ,the ture is 12 the first member of malloc(0) is 0x9e78008:0 set the first member of malloc(0) is 0x9e78008:10 after memcpy , the content is 012345678901len is 15 , the ture is 12 before malloc (nil) len of malloc(5) is 0 ,the ture is 12 the first member of malloc(5) is 0x9e78008:0 set the first member of malloc(5) is 0x9e78008:10 after memcpy , the content is 012345678901len is 15 , the ture is 12 before malloc (nil) len of malloc(20) is 0 ,the ture is 20 the first member of malloc(20) is 0x9e78018:0 set the first member of malloc(20) is 0x9e78018:10 after memcpy , the content is 012345678901 len is 12 , the ture is 20 tiger@ubuntu:/mnt/hgfs/e/Lessons/MyExercise/UtilLibs/EXERCISE$
從測試結果來看,可以得出以下幾個結論:
1. malloc(0)在我的系統里是可以正常返回一個非NULL值的。這個從申請前打印的before malloc (nil)和申請后的地址0x9e78008可以看出來,返回了一個正常的地址。
2. malloc(0)申請的空間到底有多大不是用strlen或者sizeof來看的,而是通過malloc_usable_size這個函數來看的。---當然這個函數並不能完全正確的反映出申請內存的范圍。
3. malloc(0)申請的空間長度不是0,在我的系統里它是12,也就是你使用malloc申請內存空間的話,正常情況下系統會返回給你一個至少12B的空間。這個可以從malloc(0)和malloc(5)的返回值都是12,而malloc(20)的返回值是20得到。---其實,如果你真的調用了這個程序的話,會發現,這個12確實是”至少12“的。
4. malloc(0)申請的空間是可以被使用的。這個可以從*p = 10;及memcpy(p,"01234567890123456789",12);可以得出。
雖然malloc(0)沒有發現在現實中有什么意義,但是既然有些人非要我們回答,那我們還是有必要探究一下的,否則你只有被pass掉了。關於這個問題的討論很值得,因為它讓我對技術更加感興趣,不經意間學到了其他的知識。
如果大家有什么不同意見,歡迎跟帖討論,謝謝!
注:---后為新增內容。
總結:為了安全起見,malloc(0)的非NULL返回值,最好不要進行除了free()之外的任何操作!
關於內存管理方面,大家可以參考IBM上的一篇文章:http://www.ibm.com/developerworks/cn/linux/l-memory/
非常感謝garbageMan playerc 求道於盲 五岳 懶得想一個好名字 等同仁的參與教導,小弟12年自動化專業剛畢業,知識面尚淺薄,以后有什么問題,還望博客園的各位不吝賜教,謝謝!