关于malloc(0)的返回值问题--这两天的总结与实践篇
就像我在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 -;
} //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 = ;
printf("set the first member of malloc(%d) is %p:%d \n",size,p,*p); //memcpy
memset(p,'\0',);
memcpy(p,"",);
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 = -; char *p = NULL; //malloc(0)
size = ;
alloc_memory(p,size); //malloc(5)
size = ;
alloc_memory(p,size); //malloc(20)
size = ;
alloc_memory(p,size);
return ;
}
测试结果如下:
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() is ,the ture is
the first member of malloc() is 0x9e78008:
set the first member of malloc() is 0x9e78008:
after memcpy , the content is 012345678901len is , the ture is before malloc (nil)
len of malloc() is ,the ture is
the first member of malloc() is 0x9e78008:
set the first member of malloc() is 0x9e78008:
after memcpy , the content is 012345678901len is , the ture is before malloc (nil)
len of malloc() is ,the ture is
the first member of malloc() is 0x9e78018:
set the first member of malloc() is 0x9e78018:
after memcpy , the content is len is , the ture is 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年自动化专业刚毕业,知识面尚浅薄,以后有什么问题,还望博客园的各位不吝赐教,谢谢!
关于malloc(0)的返回值问题--这两天的总结与实践篇的更多相关文章
- DWR3.0 dwr 返回值(数组,集合,Map)
首先导入项目所需要的包,如下:dwr.jar,commons-logging-1.0.4.jar,版本可以调整 1.web.xml<?xml version="1.0" en ...
- C语言中malloc函数返回值是否需要类型强制转换问题
1. 在C语言中, 如果调用的函数没有函数原型, 则其返回值将默认为 int 型. 考虑调用malloc函数时忘记了 #include <stdlib.h>的情况 此时malloc函数返回 ...
- libusb_bulk_transfer返回值不是0
libusb_bulk_transfer返回值不是0 libusb_bulk_transfer返回值不是0libusb_bulk_transfer返回值不是0 ?????
- malloc(0)分配多少内存?(译文)
原文地址:http://prog21.dadgum.com/179.html 在大多的系统中,这个C的小程序将会吸收全部空闲的内存. ){ ); } 在我们聊malloc(0)之前,让我们看看mall ...
- 【Go入门教程3】流程(if、goto、for、switch)和函数(多个返回值、变参、传值与传指针、defer、函数作为值/类型、Panic和Recover、main函数和init函数、import)
这小节我们要介绍Go里面的流程控制以及函数操作. 流程控制 流程控制在编程语言中是最伟大的发明了,因为有了它,你可以通过很简单的流程描述来表达很复杂的逻辑.Go中流程控制分三大类:条件判断,循环控制和 ...
- GetQueuedCompletionStatus的返回值
完成端口GetQueuedCompletionStatus返回值的问题 先看看GetQueuedCompletionStatus函数的完整声明:BOOL GetQueuedCompletionStat ...
- ADO.NET笔记——使用Command执行增删改操作,通过判断ExecuteNonQuery()返回值检查是否操作成功
相关知识: ExecuteNonQuery()方法:执行CommandText属性所制定的操作,返回受影响的记录条数.该方法一般用来执行SQL中的UPDATE.INSERT和DELETE等操作 对于U ...
- C++11获取线程的返回值
C++11 std::future and std::promise 在许多时候,我们会有这样的需求--即我们想要得到线程返回的值. 但是在C++11 多线程中我们注意到,std::thread对象会 ...
- python入门(14)定义函数和接收返回值
定义函数: 定义一个求绝对值的my_abs函数为例: def my_abs(x): if x >= 0: return x else: return -x 如果没有return语句,函数执行完毕 ...
随机推荐
- Zabbix故障总结(持续更新)
Zabbix housekeeper processes more than 75% busy 问题原因 为了防止数据库持续增大,zabbix有个自动删除历史数据的机制,就是housekeeper,而 ...
- xcode pod install 安装失败,提示缺少文件
I had the same problem in Xcode 6.1.1. I did the following to solve it: Set the configuration file s ...
- 第7章 网络层协议(4)_IGMP协议
4. IGMP协议(Internet Group Management Protocol) 4.1 什么是组播(多播) (1)单播同一个视频要发送90个副本,但支持收看者“快进”和“倒退”. (2)组 ...
- 图像生成-VAE简介
VAE(Variational Autoencoder) 生成式模型 理论: 基于贝叶斯公式.KL散度的推导 1. 自动编码器的一般结构 2. 产生一幅新图像 输入的数据经过神经网络降维到一个编码 ...
- ElasticSearch索引
简介 索引是具有相同结构的文档集合.在Elasticsearch中索引是个非常重要的内容,对Elasticsearch的大部分操作都是基于索引来完成的.同时索引可以类比关系型数据库Mysql中的数据库 ...
- redis参数改进建议
1.修改stop-writes-on-bgsave-error为no当前配置为yes,分别修改redis.conf和当前实例#redis.confstop-writes-on-bgsave-error ...
- js数据类型 --运算符
基本数据类型: number: var a=1; string: var str='123'; boolean: var b1=false; null:var c1=null; //打印结果为 obj ...
- hadoop的块
http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_design.html http://www.cnblogs.com/cloudma/articles/had ...
- spark historyserver 页面反应很慢 jvm堆调参
我们的spark historyserver 最近页面打开很慢 jstat -gcutil pid 1000 发现full gc 相当严重 查看堆大小,发现默认堆1G,打算修改到4G jps -lvm ...
- webform(复合控件)
一.组合单选 RadioButtonList 单选按钮与简单控件不同,可理解为在集合中放置多对象 例: <asp:RadioButtonList ID="RadioButtonList ...