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 ...
随机推荐
- mysql---union的用法
union的作用很简单用来合并两条sql的结果集 语法: SQL1 UNION SQL2 现有一张价格表 要求:求出价格低于2000和价格高于3000的商品,不能用or和not between--an ...
- javascript之变量、作用域、作用域链
一.变量 javascript的变量是松散类型的,所谓松散类型就是说定义的变量可以用来保存任何类型的数据.定义变量时要使用var操作符后面跟变量名.这里的var是一个关键字,如果定义变量时省略了var ...
- asp.net <% %>,<%# %>,<%= %>,<%$ %>区别大集合
前台页面 <div><%--可以执行服务器代码,相当于在后台写代码,Render%><%=取后台变量或方法值,只能绑定客户端控件,绑定服务器控件时后来必须调用databi ...
- discuz 重新定义jquery的$
最近做个小插件 发现加了这个代码不执行: $.ajax({ url:'plugin.php?id=register:regeist_jiangsu', type:'post', data:{ 'mob ...
- c++sort函数的用法浅析
(一)为什么要用c++标准库里的排序函数 Sort()函数是c++一种排序方法之一,学会了这种方法也打消我学习c++以来使用的冒泡排序和选择排序所带来的执行效率不高的问题!因为它使用的排序方法是类似于 ...
- 谈谈GUI的配色和字体选择
对于工控领域的显示屏,显示效果很难和电脑屏幕相比.电脑上操作系统或者各种软件的配色方案,在应用到工业控制屏上就会出现“完全不是所想要”的结果,所以工控领域GUI的色彩.字体的选择还是有特别的讲究. 配 ...
- Extjs 更新数据集Ext.PagingToolbar的start参数重置的处理
问题:当翻页后,比如当前是第二页,start参数此时是5(初始为0),当切换左侧分类时,我们期望的是从所选分类下明细记录的第一条开始显示,结果发现不是这样,依然是从新数据的第二页开始显示,就是说ext ...
- css 垂直同步的几种方式
第一种: 利用 display:table 和 display:table-cell 的方式 这种方式就好像将table布局搬过来一样,相信大家对table的td还是有印象的,它的内容是可以设置垂直居 ...
- BigDecimal除法
public class TestDemo { public static void main(String[] args) { BigDecimal dataValue = new BigDecim ...
- jQuery CSS 添加/删除类名
addClass(class) — 为每个匹配的元素添加指定的类名.参数 : class — 一个或多个要添加到元素中的CSS类名,请用空格分开(String)示例 一 :为匹配的元素加上 'sele ...