Skip to Content
C语言核心技术(原书第2版)
book

C语言核心技术(原书第2版)

by Peter Prinz, Tony Crawford
August 2017
Intermediate to advanced
798 pages
17h 44m
Chinese
China Machine Press
Content preview from C语言核心技术(原书第2版)
192
|
第
12
章
//
为一个具有
n
个
float
元素的数组分配存储空间
float *fPtr = malloc( n * sizeof(float) );
if ( fPtr == NULL )
{
/* ...
处理错误
... */
return NULL;
}
/* ... */
return fPtr;
}
将所分配的内存区域中每个字节都初始化为
0
,这种方式很有意义,它确保不只把分配
给结构对象的内存都默认地初始化为
0
,连成员之间的填补位置也是
0
。在这种情况下,
函数
calloc()
比
malloc()
更有优势,虽然在部分
C
语言实现版本中,它的运行效率不及
后者。对于要分配的内存空间,函数
calloc()
的表示方式有所区别。可以把上述示例中
的部分语句使用函数
calloc()
重新编写:
//
为一个
double
类型对象分配存储空间
double *dPtr = calloc( 1, sizeof(double) );
//
为两个
Record
类型分配存储空间
Record *rPtr;
if ( ( rPtr = calloc( 2, sizeof(Record) ) == NULL )
{ /* ...
处理内存不足错误
... */ }
//
为一个具有
n
个
float
元素的数组分配存储空间
float *fPtr = calloc( n, sizeof(float));
12.2 已分配内存的特性
如果内存分配成功,就会得到指向内存区域起始位置的指针。“起始位置”指的是该指 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

算法精讲视频课程:24篇系列讲座

算法精讲视频课程:24篇系列讲座

罗伯特·塞奇威克, 凯文·韦恩(Kevin Wayne)

Publisher Resources

ISBN: 9787111562207