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.

  1. /*
  2. Exercise 8.6. The standard library function calloc(n, size) returns a pointer to n objects
  3. of size size, with the storage initialised to zero. Write calloc, by calling
  4. malloc or by modifying it.
  5.  
  6. Author: Bryan Williams
  7.  
  8. */
  9.  
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. /*
  14. Decided to re-use malloc for this because :
  15. 1) If the implementation of malloc and the memory management layer changes, this will be ok.
  16. 2) Code re-use is great.
  17.  
  18. */
  19. void *mycalloc(size_t nmemb, size_t size)
  20. {
  21. void *Result = NULL;
  22.  
  23. /* use malloc to get the memory */
  24. Result = malloc(nmemb * size);
  25.  
  26. /* and clear the memory on successful allocation */
  27. if(NULL != Result)
  28. {
  29. memset(Result, 0x00, nmemb * size);
  30. }
  31.  
  32. /* and return the result */
  33. return Result;
  34. }
  35.  
  36. /* simple test driver, by RJH */
  37.  
  38. #include <stdio.h>
  39.  
  40. int main(void)
  41. {
  42. int *p = NULL;
  43. int i = ;
  44.  
  45. p = mycalloc(, sizeof *p);
  46. if(NULL == p)
  47. {
  48. printf("mycalloc returned NULL.\n");
  49. }
  50. else
  51. {
  52. for(i = ; i < ; i++)
  53. {
  54. printf("%08X ", p[i]);
  55. if(i % == )
  56. {
  57. printf("\n");
  58. }
  59. }
  60. printf("\n");
  61. free(p);
  62. }
  63.  
  64. return ;
  65. }

c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()的更多相关文章

  1. c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出

    Revise the main routine of the longest-line program so it will correctly print the length of arbitra ...

  2. malloc函数详解 C语言逻辑运算符

    今天写线性表的实现,又遇到了很多的难题,C语言的指针真的没学扎实.很多基础都忘了. 一是 :malloc 函数的使用. 二是:C语言逻辑运算符. 一.原型:extern void *malloc(un ...

  3. c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行

    Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...

  4. 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 ...

  5. c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格

    Write a program to copy its input to its output, replacing each string of one or more blanks by a si ...

  6. c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号

    Write a program to compare two files, printing the first line where they differ. Here's Rick's solut ...

  7. c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)

      fseek库函数 #include <stdio.h> int fseek(FILE *stream, long int offset, int origin); 返回:成功为0,出错 ...

  8. 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 ...

  9. c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件

    How would you test the word count program? What kinds of input are most likely to uncover bugs if th ...

随机推荐

  1. 超强的ACM题目类型总结

    转:初期: 一.基本算法:       (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.       ...

  2. LeetCode FindMinimuminRotatedSorteArray &&FindMinimuminRotatedSorteArray2

    LeetCode上这两道题主要是使用二分搜索解决,是二分搜索算法的一个应用,根据条件每次舍弃一半,保留一半. 首先第一题: FindMinimuminRotatedSorteArray(时间复杂度为二 ...

  3. java.util.regx Demo

    import java.util.regex.Matcher;import java.util.regex.Pattern; public class TestRegex { public stati ...

  4. Mac 系统下安装 MySql

    Mac原生没有带MySql,想要使用需要自己去安装. 下载. 首先去mysql官网下载安装包. 由于现在mysql对企业有服务,所以有所谓社区版(community)和企业版(enterprise), ...

  5. [C#]Array 添加扩展

    众所周知,Array 一旦定义好,譬如四个长度,当需要再往里面添加元素的时候,需要Array.Resize一下才可以,为了提高代码复用,所以索性封装下,方便使用,代码如下: /// <summa ...

  6. DOM五大对象

    1.Window 对象:Window 对象表示浏览器中打开的窗口. 如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个 ...

  7. jquery 里面的$(document).ready 和 DOMContentLoaded

    DOMContentLoaded 事件是当页面的初始dom tree加载好了就会执行. 而不会去等待外部的css 渲染完成和 js执行完成,即下图中的DOM Tree阶段 DOMContentLoad ...

  8. Unity3d Shader开发(三)Pass(Pass Tags,Name,BindChannels )

    Pass Tags 通过使用tags来告诉渲染引擎在什么时候该如何渲染他们所期望的效果. Syntax 语法 Tags { "TagName1" = "Value1&qu ...

  9. linux sed 批量替换多个文件中的字符

    格式: sed -i "s/查找字段/替换字段/g" `grep 查找字段 -rl 路径` linux sed 批量替换多个文件中的字符串 sed -i "s/oldst ...

  10. C# - ref & out

    引用参数和值参数 值参数,是在函数中此变量的任何修改都不影响函数调用中指定的参数,除非把它当作返回值返回,经典例子,交换两个数,但是返回值只有一个. 此时可以用引用参数,函数处理的变量和函数调用中使用 ...