C语言中经常使用的内存分配函数有malloc、calloc和realloc等三个,当中。最经常使用的肯定是malloc,这里简单说一下这三者的差别和联系。

  1、声明

  这三个函数都在stdlib.h库文件里,声明例如以下:

  void* realloc(void* ptr, unsigned newsize);

  void* malloc(unsigned size);

  void* calloc(size_t numElements, size_t sizeOfElement);

  它们的功能大致类似,就是向操作系统请求内存分配,假设分配成功就返回分配到的内存空间的地址。假设没有分配成功就返回NULL.

  2、功能

  malloc(size):在内存的动态存储区中分配一块长度为"size"字节的连续区域,返回该区域的首地址。

  calloc(n,size):在内存的动态存储区中分配n块长度为"size"字节的连续区域。返回首地址。

  realloc(*ptr,size):将ptr内存大小增大或缩小到size.

  须要注意的是realloc将ptr内存增大或缩小到size,这时新的空间不一定是在原来ptr的空间基础上,添加或减小长度来得到,而有可能(特别是在用realloc来增大ptr的内存空间的时候)会是在一个新的内存区域分配一个大空间,然后将原来ptr空间的内容复制到新内存空间的起始部分。然后将原来的空间释放掉。因此。一般要将realloc的返回值用一个指针来接收,以下是一个说明realloc函数的样例。

  #include

  #include

  int main()

  {

  //allocate space for 4 integers

  int *ptr=(int *)malloc(4*sizeof(int));

  if (!ptr)

  {

  printf("Allocation Falure!\n");

  exit(0);

  }

  //print the allocated address

  printf("The address get by malloc is : %p\n",ptr);

  //store 10、9、8、7 in the allocated space

  int i;

  for (i=0;i<4;i++)

  {

  ptr[i]=10-i;

  }

  //enlarge the space for 100 integers

  int *new_ptr=(int*)realloc(ptr,100*sizeof(int));

  if (!new_ptr)

  {

  printf("Second Allocation For Large Space Falure!\n");

  exit(0);

  }

//print the allocated address

  printf("The address get by realloc is : %p\n",new_ptr);

  //print the 4 integers at the beginning

  printf("4 integers at the beginning is:\n");

  for (i=0;i<4;i++)

  {

  printf("%d\n",new_ptr[i]);

  }

  return 0;

  }

  执行结果例如以下:

  

  从上面能够看出,在这个样例中新的空间并非以原来的空间为基址分配的,而是又一次分配了一个大的空间,然后将原来空间的内容复制到了新空间的開始部分。

  3、三者的联系

  calloc(n,size)就相当于malloc(n*size),而realloc(*ptr,size)中。假设ptr为NULL,那么realloc(*ptr,size)就相当于malloc(size)。

..................................................................................................................................

C语言内存分配函数malloc——————【Badboy】的更多相关文章

  1. 内存分配函数malloc、realloc、calloc、_alloca

    1.内存分配函数_alloca.malloc.realloc.calloc: _alloca 函数原型void * __cdecl _alloca(size_t); 头文件:malloc.h _all ...

  2. C语言内存分配函数

    c语言标准库提供了3个内存分配的函数,都包含在头文件<stdlib.h>中 1.malloc 函数原型: void *malloc( size_t size ); 参数:要分配内存大小的字 ...

  3. C语言之内存分配函数

    #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { /********* ...

  4. 【转】【C/C++】内存分配函数:malloc,calloc,realloc,_alloca

    转自:http://www.cnblogs.com/particle/archive/2012/09/01/2667034.html#commentform malloc: 原型:extern voi ...

  5. 内存管理运算符new delete与内存管理函数malloc free的区别——已经他们对对象创建的过程。

    (1)内存管理函数与内存管理运算符的区别 内存管理函数有内存分配函数,malloc calloc realloc 以及内存释放函数free. 内存管理运算符有new 和delete. 两种内存管理方式 ...

  6. Win内存分配函数(GlobalAlloc/HeapAlloc/LocalAlloc/VirtualAlloc)

    Win内存分配函数(GlobalAlloc/HeapAlloc/LocalAlloc/VirtualAlloc) 来源:http://blog.csdn.net/chunyexiyu/article/ ...

  7. 内存管理 垃圾回收 C语言内存分配 垃圾回收3大算法 引用计数3个缺点

    小结: 1.垃圾回收的本质:找到并回收不再被使用的内存空间: 2.标记清除方式和复制收集方式的对比: 3.复制收集方式的局部性优点: https://en.wikipedia.org/wiki/C_( ...

  8. C标准库-数值字符串转换与内存分配函数

    原文链接:http://www.orlion.ga/977/ 一.数值字符串转换函数 #include <stdlib.h> int atoi(const char *nptr); dou ...

  9. Linux内核中常见内存分配函数(二)

    常用内存分配函数 __get_free_pages unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order) __get_f ...

随机推荐

  1. js 字符串长度截取

    <script> function cutstr(str, len) { var temp, icount = 0, patrn = /[^\x00-\xff]/, strre = &qu ...

  2. HTTP调试工具:Fiddler介绍

    原文发布时间为:2010-08-25 -- 来源于本人的百度文章 [由搬家工具导入] 这个工具我已经使用比较长时间了,对我的帮助也挺大,今天我翻译的微软的文章,让更多的朋友都来了解这个不错的工具,也是 ...

  3. react dva 表单校验

    import React,{ Component } from 'react'; import { connect } from 'dva'; import { WhiteSpace,NavBar , ...

  4. SQL Server 2005 无法连接到 127.0.0.1

    [问题描述]如果连接本机的数据库服务器,服务器名称可填: 127.0.0.1或localhost或本机机器全名(比如HOME),有时候用本机机器名可以登录SQL Server 2005,但用127.0 ...

  5. hdu 3074(线段树)

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. jdbc in postgres

    try { Class.forName("org.postgresql.Driver").newInstance(); String url = "jdbc:postgr ...

  7. react 使用antd的在图片列表或表格中实现点击其他元素Checkbox选中功能

    antd官网上的Checkbox功能只能单独使用,在表格中加入Checkbox也只能点击Checkbox按钮才能实现选中或取消功能 如果我们要实在表格行中点击或在图片列表中点击图片就能实现选中或取消, ...

  8. HDU 2036 改革春风吹满地【计算几何/叉乘求多边形面积】

    改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. 好用的 HTTP模块SuperAgent

    SuperAgent 最近在写爬虫,看了下node里面有啥关于ajax的模块,发现superagent这个模块灰常的好用.好东西要和大家分享,话不多说,开始吧- 什么是SuperAgent super ...

  10. Codeforces 919 D Substring

    题目描述 You are given a graph with nn nodes and mm directed edges. One lowercase letter is assigned to ...