转自:http://blog.csdn.net/firecityplans/article/details/4490124/

版权声明:本文为博主原创文章,未经博主允许不得转载。

Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other.

Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big:
void *malloc( size_t size );
calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory 
at least big enough to hold them all:
void *calloc( size_t numElements, size_t sizeOfElement );
There are one major difference and one minor difference between the two functions. The major difference is that malloc() doesn't initialize the allocated memory. The first time malloc() gives you a particular chunk of memory, the memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it.
*****
 That means, unfortunately, that a program might run in simple cases (when memory is never reallocated) but break when used harder (and when memory is reused). 
*****
这句话说的意思看了2遍还是吃不透....
calloc() fills the allocated memory with all zero bits. That means that anything there you are going to use as a char or an int of any length, signed or unsigned, is guaranteed to be zero. Anything you are going to use as a pointer is set to all zero bits. 
That is usually a null pointer, but it is not guaranteed.Anything you are going to use as a float or double is set to all zero bits; that is a floating-point zero on some types of machines, but not on all. 
The minor difference between the two is that calloc() returns an array of objects; malloc() returns one object. Some people use calloc() to make clear that they want an array.

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/missuever/archive/2005/11/30/540045.aspx

另外说明:

1.分配内存空间函数malloc

  调用形式: (类型说明符*) malloc (size) 功能:在内存的动态存储区中分配一块长度为"size" 字节的连续区域。函数的返回值为该区域的首地址。 “类型说明符”表示把该区域用于何种数据类型。(类型说明符*)表示把返回值强制转换为该类型指针。“size”是一个无符号数。例如: pc=(char *) malloc (100); 表示分配100个字节的内存空间,并强制转换为字符数组类型, 函数的返回值为指向该字符数组的指针, 把该指针赋予指针变量pc。

2.分配内存空间函数 calloc

  calloc 也用于分配内存空间。调用形式: (类型说明符*)calloc(n,size) 功能:在内存动态存储区中分配n块长度为“size”字节的连续区域。函数的返回值为该区域的首地址。(类型说明符*)用于强制类型转换。calloc函数与malloc 函数的区别仅在于一次可以分配n块区域。例如: ps=(struet stu*) calloc(2,sizeof (struct stu)); 其中的sizeof(struct stu)是求stu的结构长度。因此该语句的意思是:按stu的长度分配2块连续区域,强制转换为stu类型,并把其首地址赋予指针变量ps。

简单的说是:

malloc它允许从空间内存池中分配内存,malloc()的参数是一个指定所需字节数的整数.
例如:P=(int*)malloc(n*sizeof(int));
  colloc与malloc类似,但是主要的区别是存储在已分配的内存空间中的值默认为0,使用malloc时,已分配的内存中可以是任意的值.
  colloc需要两个参数,第一个是需要分配内存的变量的个数,第二个是每个变量的大小.
例如:P=(int*)colloc(n,colloc(int));

另一个版本:

函数原型不同:
void *malloc(unsigned size)//动态申请size个字节的内存空间;功能:在内存的动态存储区中分配一块长度为" size" 字节的连续区域。函数的返回值为该区域的首地址。。(类型说明符*)表示把返回值强制转换为该类型指针。

(void *)calloc(unsigned n,unsigned size)//      用于向系统动态申请n个, 每个占size个字节的内存空间; 并把分配的内存全都初始化为零值。函数的返回值为该区域的首地址

(void *)realloc(void *p,unsigned size)//将指针p所指向的已分配内存区的大小改为size

区别:两者都是动态分配内存。主要的不同是malloc不初始化分配的内存,已分配的内存中可以是任意的值. calloc 初始化已分配的内存为0。次要的不同是calloc返回的是一个数组,而malloc返回的是一个对象。

malloc它允许从空间内存池中分配内存,          malloc()的参数是一个指定所需字节数的整数.
例如:P=(int*)malloc(n*sizeof(int));

colloc与malloc类似,    colloc需要两个参数,第一个是需要分配内存的变量的个数, 第二个是每个变量的大小.
例如:P=(int*)colloc(n,sizeof(int));

例,申请一个字符大小的指针
char *p=(char *)malloc(sizeof(char)); //当然单个是没有什么意义的申请动态数组或一个结构,如

char *str=(char *)malloc(sizeof(char)*100); //相当于静态字符数组str[100],但大小可以更改的

typedef struct pointer
{ int data; 
struct pointer *p; 
} pp;

pp *p=(pp *)malloc(sizeof(struct pointer)); //动态申请结构体空间

其他几个函数是队申请空间的修改的操作根据定义自己可以试试

再一个版本:

http://nuomi1988.blog.hexun.com/35121805_d.html

一:它们都是动态分配内存,先看看它们的原型:

void *malloc( size_t size ); //分配的大小

void *calloc( size_t numElements, size_t sizeOfElement ); // 分配元素的个数和每个元素的大小

共同点就是:它们返回的是 void * 类型,也就是说如果我们要为int或者其他类型的数据分配空间必须显式强制转换;

不同点是:用malloc分配存储空间时,必须由我们计算需要的字节数。如果想要分配5个int型的空间,那就是说需要5*sizeof(int)的内存空间:

int * ip_a;
ip_a = (int*)malloc( sizeof (int) * 5 );

而用calloc就不需要这么计算了,直接:

ip_a = ( int* )calloc( 5, sizeof(int) );这样,就分配了相应的空间,而他们之间最大的区别就是:用malloc只分配空间不初始化,也就是依然保留着这段内存里的数据,而calloc则进行了初始化,calloc分配的空间全部初始化为0,这样就避免了可能的一些数据错误。

先写段代码体验体验....

#include <iostream>

using namespace std;

void main()
{
int * ip_a;
int * ip_b;

ip_a = (int*)malloc( sizeof (int) * 5 );
for( int i = 0; i < 5; i++ )
{
   cin>>ip_a[i];
}
for( int j = 0; j < 5; j++ )
{
   cout<<ip_a[j]<<" ";
}

ip_b = ( int* )calloc( 5, sizeof(int) );
for( int j = 0; j < 5; j++ )
{
   cout<<ip_b[j]<<" ";
}

}

这个输出就能够清晰的看出他们的不同....

++版:

三个函数的申明分别是:
void* realloc(void* ptr, unsigned newsize);
void* malloc(unsigned size);
void* calloc(size_t numElements, size_t sizeOfElement);
都在stdlib.h函数库内

它们的返回值都是请求系统分配的地址,如果请求失败就返回NULL

malloc用于申请一段新的地址,参数size为需要内存空间的长度,如:
char* p;
p=(char*)malloc(20);

calloc与malloc相似,参数sizeOfElement为申请地址的单位元素长度,numElements为元素个数,如:
char* p;
p=(char*)calloc(20,sizeof(char));
这个例子与上一个效果相同

realloc是给一个已经分配了地址的指针重新分配空间,参数ptr为原有的空间地址,newsize是重新申请的地址长度
如:
char* p;
p=(char*)malloc(sizeof(char)*20);
p=(char*)realloc(p,sizeof(char)*40);

注意,这里的空间长度都是以字节为单位。

C语言的标准内存分配函数:malloc,calloc,realloc,free等。
malloc与calloc的区别为1块与n块的区别:
malloc调用形式为(类型*)malloc(size):在内存的动态存储区中分配一块长度为“size”字节的连续区域,返回该区域的首地址。
calloc调用形式为(类型*)calloc(n,size):在内存的动态存储区中分配n块长度为“size”字节的连续区域,返回首地址。
realloc调用形式为(类型*)realloc(*ptr,size):将ptr内存大小增大到size。
free的调用形式为free(void*ptr):释放ptr所指向的一块内存空间。
C++中为new/delete函数。

malloc()与calloc区别【转】的更多相关文章

  1. malloc()与calloc区别 (转)

    另外说明: 1.分配内存空间函数malloc 调用形式: (类型说明符*) malloc (size) 功能:在内存的动态存储区中分配一块长度为"size" 字节的连续区域.函数的 ...

  2. [reprint]malloc与calloc的区别

    [http://blog.163.com/crazy20070501@126/] 转自某自由人的博客: malloc与calloc的区别 函数malloc()和calloc()都可以用来动态分配内存空 ...

  3. 【C/C++开发】malloc,calloc和realloc的区别和注意事项

    (1)C语言跟内存分配方式 <1>从静态存储区域分配.        内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量.static变量. <2&g ...

  4. C语言中malloc()和calloc()c函数用法

    C语言中malloc()和calloc()c函数用法   函数malloc()和calloc()都可以用来动态分配内存空间,但两者稍有区别. malloc()函数有一个参数,即要分配的内存空间的大小: ...

  5. malloc,calloc,realloc,alloc

    三个函数的申明分别是: void* realloc(void* ptr, unsigned newsize); void* malloc(unsigned size); void* calloc(si ...

  6. malloc,calloc,realloc函数用法,原理及不同解析

    https://blog.csdn.net/lixungogogo/article/details/50887028 一.malloc malloc在MSDN中原型为: void *malloc( s ...

  7. malloc realloc calloc free

    自上次发现自己对这几个C函数不熟悉,就打算抽空整理一下,也就现在吧.这几个函数都是跟堆内存打交道的,还有一个好玩的函数--alloca,它是跟栈内存打交道的,我想留在以后研究出好玩点的来,再专门为其写 ...

  8. Linux C 堆内存管理函数malloc()、calloc()、realloc()、free()详解

    C 编程中,经常需要操作的内存可分为下面几个类别: 堆栈区(stack):由编译器自动分配与释放,存放函数的参数值,局部变量,临时变量等等,它们获取的方式都是由编译器自动执行的 堆区(heap):一般 ...

  9. malloc()与calloc差别

    Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slig ...

随机推荐

  1. 大话CNN经典模型:VGGNet

       2014年,牛津大学计算机视觉组(Visual Geometry Group)和Google DeepMind公司的研究员一起研发出了新的深度卷积神经网络:VGGNet,并取得了ILSVRC20 ...

  2. 10,knn手写数字识别

    # 导包 import numpy as np import matplotlib.pyplot as plt from sklearn.neighbors import KNeighborsClas ...

  3. Android获取状态栏的高度:

    方法一: //获取手机状态栏高度 public static int getStatusBarHeight(Context context){ Class<?> c = null; Obj ...

  4. luogu3317 [SDOI2014]重建

    原来矩阵树定理对于边是概率的情况也是适用的qwqwq. ref #include <iostream> #include <cstdio> #include <cmath ...

  5. Python学习4,字符串

    字符串这个东西主要靠记,多打打就好了. _author_ = "Happyboy" name = "my \tname is happyboy and i am 66 y ...

  6. day01--python基础1

    # 01讲   - Windows下执行程序,必须加 PYTHON.在LINUX下,可以不指明是PYTHON.但是,执行钱许给予hello.py执行权限. - 其次,只要变成可执行程序,必须第一行事前 ...

  7. Python全栈 MySQL 数据库 (表字段增、删、改、查、函数)

    ParisGabriel              每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰    开局一张图         查询SQL变量 show variables 1.表字 ...

  8. php 值传递和引用传递的区别

    值传递:函数范围内对值的任何改变在函数外部都会被忽略 引用传递:函数范围内对值的任何改变在函数外部也能反映出这些修改 A:按值传递时,php必须复制值.特别是对于大型的字符串和对象来说,这将会是一个代 ...

  9. Scala 基础(5)—— 构建函数式对象

    有了 Scala 基础(4)—— 类和对象 的前提,现在就可以来构建一个基于 Scala 的函数式对象. 下面开始构造一个有理数对象 Rational. 1. 主构造方法和辅助构造方法 对于每一个类的 ...

  10. 关于flink的时间处理不正确的现象复现&原因分析

    跟朋友聊天,说输出的时间不对,之前测试没关注到这个,然后就在processing模式下看了下,发现时间确实不正确 然后就debug,看问题在哪,最终分析出了原因,记录如下:    最下面给出了复现方案 ...