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与验证控件结合验证

    原文发布时间为:2010-05-14 -- 来源于本人的百度文章 [由搬家工具导入] 把BUTTOn中 的return CheckFive()去 掉 .. 放 到 <Form id=" ...

  2. hdu 6218 Bridge 线段树 set

    题目链接 题意 给一个\(2\)x\(n\)的矩阵,每个格子看成一个点,每个格子与相邻的格子间有边.现进行一些加边与删边操作,问每次操作后图中有多少条割边. 思路 参考 https://www.cnb ...

  3. poj 2528 Mayor's posters 线段树 || 并查集 离线处理

    题目链接 题意 用不同颜色的线段覆盖数轴,问最终数轴上有多少种颜色? 注:只有最上面的线段能够被看到:即,如果有一条线段被其他的线段给完全覆盖住,则这个颜色是看不到的. 法一:线段树 按题意按顺序模拟 ...

  4. Swift Perfect 基础项目

    brew install mysql@5.7 && brew link mysql@5.7 --force Package.swift import PackageDescriptio ...

  5. hdu 3657 最小割的活用 / 奇偶方格取数类经典题 /最小割

    题意:方格取数,如果取了相邻的数,那么要付出一定代价.(代价为2*(X&Y))(开始用费用流,敲升级版3820,跪...) 建图:  对于相邻问题,经典方法:奇偶建立二分图.对于相邻两点连边2 ...

  6. hdu 2824(欧拉函数)

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. HDU 6227.Rabbits-规律 (2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学))

    Rabbits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total S ...

  8. sprak 环境搭建的坑

    1,/etc/hosts下的ip master/slave 的对照 /etc/sysconfig/network 配置: NETWORKING=yes HOSTNAME=master 以及spark/ ...

  9. NLP--edit distance

    基本思想 通过插入(insert).删除(delete)和替换(substitute)个操作将一个字符串s1变换到另一个字符串s2的最少步骤数distacnce,用(1-distance/length ...

  10. luogu P1579 哥德巴赫猜想(升级版)

    题目描述 一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列. 在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集合(双 ...