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函數。

相關文章
相關標籤/搜索