c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()
The standard library function calloc(n,size)
returns a pointer to n
objects of size size
, with the storage initialized to zero. Write calloc
, by calling malloc
or by modifying it.
- /*
- Exercise 8.6. The standard library function calloc(n, size) returns a pointer to n objects
- of size size, with the storage initialised to zero. Write calloc, by calling
- malloc or by modifying it.
- Author: Bryan Williams
- */
- #include <stdlib.h>
- #include <string.h>
- /*
- Decided to re-use malloc for this because :
- 1) If the implementation of malloc and the memory management layer changes, this will be ok.
- 2) Code re-use is great.
- */
- void *mycalloc(size_t nmemb, size_t size)
- {
- void *Result = NULL;
- /* use malloc to get the memory */
- Result = malloc(nmemb * size);
- /* and clear the memory on successful allocation */
- if(NULL != Result)
- {
- memset(Result, 0x00, nmemb * size);
- }
- /* and return the result */
- return Result;
- }
- /* simple test driver, by RJH */
- #include <stdio.h>
- int main(void)
- {
- int *p = NULL;
- int i = ;
- p = mycalloc(, sizeof *p);
- if(NULL == p)
- {
- printf("mycalloc returned NULL.\n");
- }
- else
- {
- for(i = ; i < ; i++)
- {
- printf("%08X ", p[i]);
- if(i % == )
- {
- printf("\n");
- }
- }
- printf("\n");
- free(p);
- }
- return ;
- }
c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()的更多相关文章
- c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出
Revise the main routine of the longest-line program so it will correctly print the length of arbitra ...
- malloc函数详解 C语言逻辑运算符
今天写线性表的实现,又遇到了很多的难题,C语言的指针真的没学扎实.很多基础都忘了. 一是 :malloc 函数的使用. 二是:C语言逻辑运算符. 一.原型:extern void *malloc(un ...
- c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行
Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...
- c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
Write a program to print a histogram of the lengths of words in its input. It is easy to draw the hi ...
- c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格
Write a program to copy its input to its output, replacing each string of one or more blanks by a si ...
- c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号
Write a program to compare two files, printing the first line where they differ. Here's Rick's solut ...
- c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)
fseek库函数 #include <stdio.h> int fseek(FILE *stream, long int offset, int origin); 返回:成功为0,出错 ...
- c程序设计语言_习题1-19_编写函数reverse(s)将字符串s中字符顺序颠倒过来。
Write a function reverse(s) that reverses the character string s . Use it to write a program that re ...
- c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件
How would you test the word count program? What kinds of input are most likely to uncover bugs if th ...
随机推荐
- 超强的ACM题目类型总结
转:初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ...
- LeetCode FindMinimuminRotatedSorteArray &&FindMinimuminRotatedSorteArray2
LeetCode上这两道题主要是使用二分搜索解决,是二分搜索算法的一个应用,根据条件每次舍弃一半,保留一半. 首先第一题: FindMinimuminRotatedSorteArray(时间复杂度为二 ...
- java.util.regx Demo
import java.util.regex.Matcher;import java.util.regex.Pattern; public class TestRegex { public stati ...
- Mac 系统下安装 MySql
Mac原生没有带MySql,想要使用需要自己去安装. 下载. 首先去mysql官网下载安装包. 由于现在mysql对企业有服务,所以有所谓社区版(community)和企业版(enterprise), ...
- [C#]Array 添加扩展
众所周知,Array 一旦定义好,譬如四个长度,当需要再往里面添加元素的时候,需要Array.Resize一下才可以,为了提高代码复用,所以索性封装下,方便使用,代码如下: /// <summa ...
- DOM五大对象
1.Window 对象:Window 对象表示浏览器中打开的窗口. 如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个 ...
- jquery 里面的$(document).ready 和 DOMContentLoaded
DOMContentLoaded 事件是当页面的初始dom tree加载好了就会执行. 而不会去等待外部的css 渲染完成和 js执行完成,即下图中的DOM Tree阶段 DOMContentLoad ...
- Unity3d Shader开发(三)Pass(Pass Tags,Name,BindChannels )
Pass Tags 通过使用tags来告诉渲染引擎在什么时候该如何渲染他们所期望的效果. Syntax 语法 Tags { "TagName1" = "Value1&qu ...
- linux sed 批量替换多个文件中的字符
格式: sed -i "s/查找字段/替换字段/g" `grep 查找字段 -rl 路径` linux sed 批量替换多个文件中的字符串 sed -i "s/oldst ...
- C# - ref & out
引用参数和值参数 值参数,是在函数中此变量的任何修改都不影响函数调用中指定的参数,除非把它当作返回值返回,经典例子,交换两个数,但是返回值只有一个. 此时可以用引用参数,函数处理的变量和函数调用中使用 ...