malloc(0)返回什么地址?
来源:互联网 发布:ug编程好学吗 编辑:程序博客网 时间:2024/06/06 07:06
转载自:点击打开链接
问题内容:malloc(0)返回什么地址?
char *p = (char *)malloc(0);strcpy(p, "hello");printf("%s/n",p);free(p);
其中,p中的地址是堆内的首地址?
---------------------------------------------------------------
C99标准(ISO/IEC 9899:1999 (E))上说:
If the size of the space requested is zero, the behavior is implementationdefined:
either a null pointer is returned, or the behavior is as if the size were some
nonzero value, except that the returned pointer shall not be used to access an object.
如果所请求的空间大小为0,其行为由库的实现者定义:可以返回空指针,也可以让效果跟申请某个非0大小的空间一样,所不同的是返回的指针不可以被用来访问一个对象。
为什么 new T[0] 和 malloc(0) 不返回空指针
首先需要说明的是,按照C++标准,成功的 new T[0] 是不能返回空指针的。而 malloc(0),C 语言标准则指出,成功的时候可以返回空指针,也可以返回非空指针,多数库一般也选择了返回非空指针这种行为。
为什么这么做呢?
1. 理念:0大小的对象也是对象。
2. 实践:返回空会和分配失败混淆,尤其是大小是计算出来的时候,这时如果得到的是空指针,用户程序会误以为分配失败了。
3. 实现:反正返回的地址不能读写,此时可以返回同一个固定的地址,并没什么额外开销。而且多数编译器,C和C++一起提供,C++库的new也用C库的malloc实现,使二者保持一致也比较省事。
4.不管返回的是不是空,都可以free的。
测试程序如下
#include <stdlib.h>#include <stdio.h>int main(int argc, char *argv[]){char *ptr;printf("0x%x/n", ptr); // this line will cause seg fault sometimesprintf("----malloc 0 4 times----/n");ptr = (char *)malloc(0);printf("0x%x/n", ptr);ptr = (char *)malloc(0);printf("0x%x/n", ptr);ptr = (char *)malloc(0);printf("0x%x/n", ptr);ptr = (char *)malloc(0);printf("0x%x/n", ptr);printf("----malloc 1 4 times----/n");ptr = (char *)malloc(1);printf("0x%x/n", ptr);ptr = (char *)malloc(1);printf("0x%x/n", ptr);ptr = (char *)malloc(1);printf("0x%x/n", ptr);ptr = (char *)malloc(1);printf("0x%x/n", ptr);printf("----malloc 4 4 times----/n");ptr = (char *)malloc(4);printf("0x%x/n", ptr);ptr = (char *)malloc(4);printf("0x%x/n", ptr);ptr = (char *)malloc(4);printf("0x%x/n", ptr);ptr = (char *)malloc(4);printf("0x%x/n", ptr);printf("----malloc 16 4 times----/n");ptr = (char *)malloc(16);printf("0x%x/n", ptr);ptr = (char *)malloc(16);printf("0x%x/n", ptr);ptr = (char *)malloc(16);printf("0x%x/n", ptr);ptr = (char *)malloc(16);printf("0x%x/n", ptr);printf("----malloc 32 4 times----/n");ptr = (char *)malloc(32);printf("0x%x/n", ptr);ptr = (char *)malloc(32);printf("0x%x/n", ptr);ptr = (char *)malloc(32);printf("0x%x/n", ptr);ptr = (char *)malloc(32);printf("0x%x/n", ptr);printf("----malloc 64 4 times----/n");ptr = (char *)malloc(64);printf("0x%x/n", ptr);ptr = (char *)malloc(64);printf("0x%x/n", ptr);ptr = (char *)malloc(64);printf("0x%x/n", ptr);ptr = (char *)malloc(64);printf("0x%x/n", ptr);return 0;}
系统表现如下:
root@dell:~/test# ./a.out .0xd27fb840----malloc 0 4 times----0x8060f380x8060f480x8060f580x8060f68 diff=0x10=16B----malloc 1 4 times----0x8060f780x8060f880x8060f980x8060fa8 diff=0x10=16B----malloc 4 4 times----0x8060fb80x8060fc80x8060fd80x8060fe8 diff=0x10=16B----malloc 16 4 times----0x80613400x80613580x80613700x8061388 diff=0x18=24B----malloc 32 4 times----0x80619480x80619700x80619980x80619c0 diff=0x28=40B----malloc 64 4 times----0x80623480x80623900x80623d80x8062420 diff=0x48=72B
第一个是0xdxxx是因为指针变量在建栈的时候已经在栈上获得4B的一个空间,而空间里面的值,应该是随机的
不过不晓得为什么总指在同一个地方?
malloc在ics(introduction to computer science)中讲过,除了分出来的空间之外,为了维护整个堆结构,并考虑时间,空间效果
其或头,或尾,或头尾,均有额外的指针结构存在
分析16,32,64B的情况可知,多余的部分为8B,猜测是头尾各4B
然后再看,0,1,4的时候都是16B,说明每次分配的时候有个最小下限
也就是起板就是8B作为分出来的数据,另外8B作为额外的指针什么的。
- malloc(0)返回什么地址?
- malloc(0)返回什么地址?
- malloc(0)返回什么地址?
- malloc(0)返回什么地址?
- malloc(0)返回什么地址?
- malloc(0)返回什么地址?
- malloc(0)返回什么
- malloc(0)返回什么
- malloc(0)返回值
- 封装malloc编写一个malloc16使得返回地址%16==0
- malloc(0)会发生什么?
- malloc(0) 的返回值
- malloc(0)的返回值
- 返回动态内存--malloc
- 返回动态内存--malloc
- malloc函数申请内存失败,返回空NULL(0)
- 编写支持对其分配的malloc和free函数,分配内存时,malloc函数返回的地址必须能被2的n次方整除
- malloc 返回动态内存问题
- ASP.NET,web.config 中SessionState的配置
- C++四种强制类型转换
- 特殊点到三角形三个顶点的和
- ActivityManager框架解析
- Please make sure the -vm option in eclipse.ini is pointing to a JDK and verify that Installed JRE’s
- malloc(0)返回什么地址?
- 【ASP.net文档】用C#实现HTTP协议下的多线程文件传输
- 编程如何在墙上开圆形洞口
- Overlay Surfaces (覆盖表面)
- Eclipse中Outline和Package Explorer中显示的小图标的意义
- 【转载】Java中得日期处理【含日期的加和减等】
- 自动调节MSHFlexGrid控件的行高列宽
- dwr下的json数据的使用
- FCKEditor