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. VS2010 error RC2135: file not found

    VS2010 C++ win32 DLL 工程, 添加 rc 文件, 编辑 String Table. 默认情况下英文版本的 rc 文件能够顺序编译通过,为了让工程支持多语言,将字符串修改为其他语言时 ...

  2. java中instanceof和getClass()的作用

    初学者难免有点混淆java中instanceof和getClass()的作用,  下面就来一一讲解. 父类A: class A { } 子类B: class B extends A { }  构造对象 ...

  3. arguments.callee 调用自身

    一.Arguments该对象代表正在执行的函数和调用他的函数的参数.[function.]arguments[n]参数function :选项.当前正在执行的 Function 对象的名字.n :选项 ...

  4. input效果:当鼠标在input中输入文字是改变内部文字效果

    主要用到属性:onpropertychange事件(属性改变时触发的事件),oninput属性(当input有输入时发生的事件) onpropertychange事件是IE专属事件 oninput属性 ...

  5. Jquery Offset, Document, Window 都是什么

    From http://www.cnblogs.com/luhe/archive/2012/11/14/2769263.html   JQuery Offset实验与应用 我们有时候需要实现这样一种功 ...

  6. 关于实现判断用户是在PC端和还是移动端访问。

    最近一直在忙我们团队的项目“咖啡之翼”,在这个项目中,我们为移动平台提供了一个优秀的体验.伴随Android平台的红火发展.不仅带动国内智能手机行业,而且许多国内开发者也开始投身于Android移动终 ...

  7. #Leet Code# Unique Path(todo)

    描述: 使用了递归,有些计算是重复的,用了额外的空间,Version 1是m*n Bonus:一共走了m+n步,例如 m = 2, n = 3 [#, @, @, #, @],所以抽象成数学问题,解是 ...

  8. Vijos P1061 迎春舞会之三人组舞 DP

    题目链接:https://vijos.org/p/1061 n个人选出3*m人,排成m组,每组3人. 站的队形——较矮的2个人站两侧,最高的站中间. 从对称学角度来欣赏,左右两个人的身高越接近,则这一 ...

  9. 用android-x86模拟器不能运行程序错误Tag:libc的问题

    如果用的是x86的android模拟器,运行软件时一闪就关闭logcat中:类似:Fatal signal 11 (SIGSEGV) at 0x00000078 (code=1), thread 16 ...

  10. To fix sql server 2008 r2 Evaluation period has expired by change the key

    PTTFM-X467G-P7RH2-3Q6CG-4DMYB 数据中心版:PTTFM-X467G-P7RH2-3Q6CG-4DMYB   测试可用 开 发者 版:MC46H-JQR3C-2JRHY-XY ...