malloc()與calloc區別

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.