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()的更多相关文章

  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. ajax详解,以及异步JSOP的实现

    这里我使用的是jquery的ajax方法   包括三个方法 : get() , post(),   getJson() get() 和post()的格式我就使用一下格式,很方便: $.ajax({ u ...

  2. [转]怎样在cmd(命令提示符)下进行复制粘贴操作

    原文链接:http://jingyan.baidu.com/article/93f9803fd3a4dde0e46f55f5.html cmd下复制粘贴的快捷操作方式 工具/原料 系统cmd 步骤/方 ...

  3. 建造者模式(Builder Pattern)

    建造者模式:使用多个简单对象一步步构建成一个复杂的对象. 有时候,我们会创建一个“复杂”的对象,这个对象的由很多子对象构成,由于需求的变化,这个对象的各个部分经常面临剧烈的变化. 继续工厂模式的披萨店 ...

  4. (转)web.config 为某个单独页面设置编码格式

    原文链接:http://www.cnblogs.com/mytechblog/articles/1937407.html 全站应用utf-8格式,在web.config里的<system.web ...

  5. Apache的多路处理模块MPM:Prefork Worker Event

    如何确认当前apache使用哪种模式 通过/etc/init.d/httpd中的来确认系统apache的运行脚本路径 apachectl=/usr/sbin/apachectl httpd=${HTT ...

  6. python正则实例

    # -*- coding: cp936 -*-import reidcardregex=r"^[1-9]\d{14}(\d{2}[0-9x])?$"print re.search( ...

  7. phpcms v9后台多表查询分页代码

    phpcms v9里面自带的listinfo分页函数蛮好用的,可惜啊.不支持多表查询并分页. 看了一下前台模板层支持get标签,支持多表查询,支持分页.刚好可以把这个功能搬到后台来使用. 我们现在对g ...

  8. OpenSessionInViewFilter与org.springframework.dao.InvalidDataAccessApiUsageException

    报错:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in r ...

  9. python3编码问题

    继续收集python3编码问题相关资料 资料来源  鹏程的新浪博客(转载)http://blog.sina.com.cn/s/blog_6d7cf9e50102vo90.html  这篇鹏程老师写的关 ...

  10. CentOS添加RPMforge软件源

    1.查看CentOS版本 cat /etc/redhat-release 2.查看系统位数 uname -i 3.下载rpm包 wget "rpm地址" CentOS 6: i68 ...