惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
D
Docker
N
Netflix TechBlog - Medium
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
L
LangChain Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
博客园_首页
量子位
罗磊的独立博客
Blog — PlanetScale
Blog — PlanetScale
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
D
DataBreaches.Net
I
InfoQ
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
H
Help Net Security
V
V2EX

博客园 - a斗

三个阶段 【ZZ】UDP和TCP 大全 白居易--《祭李侍郎文》 【ZZ】VS2005的快捷键大全 正则表达式学习(一) 【ZZ】正则表达式的学习 C#窗体不显示在任务栏中 巴菲特的一些言论 【zz】C#的三层结构 [zz]SqlServer数据类型 随笔--1.19 很累 【ZZ】OSI模型 【ZZ】IPv4与IPv6 论文快结束了~~ 辛弃疾--《南乡子·登京口北固亭有怀》 坚持 inline的小结 (ZZ~~)字符串单词顺序反转
malloc和calloc的区别(笔试题)
a斗 · 2007-12-22 · via 博客园 - a斗

虽然见过,但是很少用calloc,还是代码量少啊。。。
区别1:函数形式:
void *calloc(
   size_t num,   //numbers of elements
   size_t size   //Length in bytes of each elements
);

void *malloc(
size_t size  //Bytes to alloc
);
区别2:calloc分配内存后会自动初始化为0,而malloc不会。
还有一道题:判断程序能不能执行:
 void *p=(void *)malloc(void);
 p++;
这个是不能执行的,参数为void说明没有参数。malloc不支持0参数。
若改为:void *p=(void *)malloc(0);在VC6下不会返回空指针,引用网上找的一句C99标准:
If the size of the space requested is zero, the behavior is  implementation defined:   

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大小的空间一样,所不同的是返回的指针不可以被用来访问一个对象。

如果是void *p=(void *)malloc(-1);由于在32位系统size_t被定义为unsigned int,在64位系统被定义
为unsigned long故分配一个很大的数。
但是第二句将一个void*加加,编译出错:“error C2036: 'void *' : unknown size”。我居然想当然
的认为它可以转为int*。。。