国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
malloc和calloc區(qū)別 - fengxue1127的日志 - 網(wǎng)易博客

malloc和calloc區(qū)別

VC++6.02008-07-21 10:24:49閱讀43評論0  字號: 訂閱

calloc(m, n) 本質(zhì)上等價于

p = malloc(m * n); memset(p, 0, m * n);

填充的零是全零, 因此不能確保生成有用的空指針值或浮點零值,free() 可以安全地用來釋放 calloc() 分配的內(nèi)存。

malloc()和calloc()都是用來分配動態(tài)內(nèi)存的函數(shù),兩者的操作有以下的區(qū)別:

void *malloc( size_t size );

malloc()以分配的內(nèi)存大小size為參數(shù)返回一個指針,該指針指向一塊最小值為size的內(nèi)存區(qū)域。

void *calloc( size_t numElements, size_t sizeOfElement );

calloc的參數(shù)為一組元素和每個元素的size,返回的指針指向的內(nèi)存至少可以存儲所有的這些元素單元。

兩者間有一個主要和次要的區(qū)別。主要區(qū)別為malloc不初始化分配的內(nèi)存區(qū)域。第一次調(diào)用malloc返回的內(nèi)存可能初值為0,如果該內(nèi)存被分配、釋放然后又重新分配,那么存儲區(qū)可能存儲的就是殘留的垃圾數(shù)據(jù)。也就是說,程序可能在內(nèi)存不重新分配的簡單情況下可以運行,不幸的是如果內(nèi)存可以重分配,程序就會異常。calloc會初始化分配的內(nèi)存為0,即你將使用的任何東西,不管是字符還是任何長度,有符號或無符號的整數(shù)都是0。你的指針也都指向為0的字節(jié)位。通常是一個空指針,但并不保證總是這樣。浮點數(shù)和雙精度型的數(shù)據(jù)也都是0,有的機(jī)器上有值為0的浮點數(shù)指針,但不普遍。

兩者次要的區(qū)別為calloc返回一串對象,malloc返回一個對象,當(dāng)強(qiáng)調(diào)想返回一個隊列時,應(yīng)該調(diào)用calloc。(別人翻譯的)

calloc和malloc

 

原型:extern void *calloc(int num_elems, int elem_size)
用法:#include
功能:為具有num_elems個長度為elem_size元素的數(shù)組分配內(nèi)存
說明:如果分配成功則返回指向被分配內(nèi)存的指針,否則返回空指針NULL。
當(dāng)內(nèi)存不再使用時,應(yīng)使用free()函數(shù)將內(nèi)存塊釋放。
舉例:
// calloc.c
 #include
#include
main()
{
               char *p;
               clrscr();        // clear screen
               p=(char *)calloc(100,sizeof(char));
            if(p)

          printf("Memory Allocated at: %x",p);

        else

          printf("Not Enough Memory! ");

         

        free(p);

        getchar();

        return 0;

      }

     

malloc

 

原型:extern void *malloc(unsigned int num_bytes);

用法:#include

功能:分配長度為num_bytes字節(jié)的內(nèi)存塊

說明:如果分配成功則返回指向被分配內(nèi)存的指針,否則返回空指針NULL。 當(dāng)內(nèi)存不再使用時,應(yīng)使用free()函數(shù)將內(nèi)存塊釋放。

舉例:

 // malloc.c

 #include

 #include

main()

 {

        char *p; clrscr(); // clear screen

         p=(char *)malloc(100);

         if(p) printf("Memory Allocated at: %x",p);

         else printf("Not Enough Memory! ");

          free(p);

         getchar();

         return 0;

}

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

0  分享到:      
閱讀(43)|評論(0)|引用(0)|舉報
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
calloc(), malloc(), realloc(), free() - 鏡花水月 ...
malloc()與calloc區(qū)別
realloc函數(shù)
動態(tài)內(nèi)存分配,地址對齊
動態(tài)內(nèi)存分配
3.4 動態(tài)內(nèi)存分配 (Dynamic memory)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服