malloc與calloc區別【存儲在已分配的內存空間中的值】


一下有幾個版本的malloc與calloc區別的解釋。都收集了過來,有興趣的朋友可以參看下。主要區別我總結就是:

 colloc與malloc類似,但是主要的區別是存儲在已分配的內存空間中的值默認爲0,這樣就避免了可能的一些數據錯誤。使用malloc時,已分配的內存中可以是任意的值.

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). 
*****
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.


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)); //動態申請結構體空間

其他幾個函數是隊申請空間的修改的操作根據定義自己可以試試

 

 

再一個版本:

一:它們都是動態分配內存,先看看它們的原型:

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]<<" ";
}


}

這個輸出就能夠清晰的看出他們的不同....

 

 

C++版:

三個函數的申明分別是:
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函數。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章