malloc()与calloc差异

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


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. ios

本文来自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。spa

 

简单的说是:.net

 

malloc它赞成从空间内存池中分配内存,malloc()的參数是一个指定所需字节数的整数.
好比:P=(int*)malloc(n*sizeof(int));
  colloc与malloc类似,但是基本的差异是存储在已分配的内存空间中的值默以为0,使用malloc时,已分配的内存中可以是随意的值.
  colloc需要两个參数,第一个是需要分配内存的变量的个数,第二个是每个变量的大小.
好比:P=(int*)colloc(n,colloc(int));指针

 

还有一个版本号:htm

 

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

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

(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函数。

相关文章
相关标签/搜索