关于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语句,函数执行完毕 ...
随机推荐
- 阿里云:游戏行业DDoS攻击解决方案
转自:http://www.gamelook.com.cn/2018/01/319420 根据全球游戏和全球移动互联网行业第三方分析机构Newzoo的数据显示:2017年上半年,中国以275亿美元的游 ...
- linux下的网络通信设置:openssh、PuTTY、tightVNC
OpenSSH的安装: windows上安装PuTTY: PuZZY上传文件到linux: 1.在window下的cmd中cd到PuZZY所在的文件夹下 2.使用pscp命令上传文件 3.使用psc ...
- 动态添加XtraTabControl的page页和子窗体
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...
- centos7 安装Node.js并配置为全局可用
本文Node.js版本为5.12.0,登录 https://nodejs.org/dist/v5.12.0/,需指定其他版本的话可以直接修改版本号进行登录. 为了方便使用tar命令对文件进行解压,我们 ...
- 微信小程序如何引用其他js文件
1.我们先建立一个common.js文件,在common.js编写我们的程序, function myfunc() { console.log("myfunc....");} mo ...
- day10学习笔记整理
函数对象函数是第一类对象: 指的是函数名指向的值(函数)可以被当作数据去使用 1. 可以被引用 2. 可以当作参数传给另外一个函数 3. 可以当作一个函数的返回值 4. 可以当作容器类型的元素 l ...
- SpringBoot 实现前后端分离的跨域访问(CORS)
序言:跨域资源共享向来都是热门的需求,使用CORS可以帮助我们快速实现跨域访问,只需在服务端进行授权即可,无需在前端添加额外设置,比传统的JSONP跨域更安全和便捷. 一.基本介绍 简单来说,CORS ...
- 原生javascript AJAX 三级联动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- StanFord ML 笔记 第四部分
第四部分: 1.生成学习法 generate learning algorithm 2.高斯判别分析 Gaussian Discriminant Analysis 3.朴素贝叶斯 Navie Baye ...
- uva-10391-枚举
题意:对于输入的字符串,判断是否存在一个单词a=b+c 俩种方法,枚举每一个单词进行拼接,复杂度是n*n 枚举每一个单词,对单词进行substr,判断substr出来的是不在map里面 #includ ...