内存分配函数举例

内存分配函数举例

ID:10210787

大小:25.50 KB

页数:3页

时间:2018-06-12

内存分配函数举例_第1页
内存分配函数举例_第2页
内存分配函数举例_第3页
资源描述:

《内存分配函数举例》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、函数名:malloc功 能:内存分配函数用 法:void*malloc(unsignedsize);程序例:#include#include#include#includeintmain(void){  char*str;  /*allocatememoryforstring*/  /*Thiswillgenerateanerrorwhencompiling*/  /*withC++,usethenewoperatorinstead.*/  if((str=malloc(10))==NULL)  {     printf("Notenoughmemorytoallocatebuffer")

2、;     exit(1); /*terminateprogramifoutofmemory*/  }  /*copy"Hello"intostring*/  strcpy(str,"Hello");  /*displaystring*/  printf("Stringis%s",str);  /*freememory*/  free(str);  return0;}函数名:calloc功 能:分配主存储器用 法:void*calloc(size_tnelem,size_telsize);程序例:#include#includeintmain(void){  char*str=NULL; 

3、 /*allocatememoryforstring*/  str=calloc(10,sizeof(char));  /*copy"Hello"intostring*/  strcpy(str,"Hello");  /*displaystring*/  printf("Stringis%s",str);  /*freememory*/  free(str);  return0;}函数名:free功 能:释放已分配的块用 法:voidfree(void*ptr);程序例:#include#include#includeintmain(void){  char*str;  /*allocat

4、ememoryforstring*/  str=malloc(10);  /*copy"Hello"tostring*/  strcpy(str,"Hello");  /*displaystring*/  printf("Stringis%s",str);  /*freememory*/  free(str);  return0;}

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。