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

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
Security Latest
Security Latest
P
Privacy International News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AI
AI
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
S
Secure Thoughts
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
D
DataBreaches.Net
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
罗磊的独立博客
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
V
V2EX
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tenable Blog
T
Threat Research - Cisco Blogs
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
Lohrmann on Cybersecurity
F
Full Disclosure
H
Help Net Security
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
A
Arctic Wolf
O
OpenAI News
S
Securelist

博客园 - 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*。。。