Difference between malloc and calloc?

后端 未结 14 1479
感情败类
感情败类 2020-11-22 03:40

What is the difference between doing:

ptr = (char **) malloc (MAXELEMS * sizeof(char *));

or:

ptr = (char **) calloc (MAXEL         


        
相关标签:
14条回答
  • 2020-11-22 04:03

    Difference 1:

    malloc() usually allocates the memory block and it is initialized memory segment.

    calloc() allocates the memory block and initialize all the memory block to 0.

    Difference 2:

    If you consider malloc() syntax, it will take only 1 argument. Consider the following example below:

    data_type ptr = (cast_type *)malloc( sizeof(data_type)*no_of_blocks );
    

    Ex: If you want to allocate 10 block of memory for int type,

    int *ptr = (int *) malloc(sizeof(int) * 10 );
    

    If you consider calloc() syntax, it will take 2 arguments. Consider the following example below:

    data_type ptr = (cast_type *)calloc(no_of_blocks, (sizeof(data_type)));
    

    Ex: if you want to allocate 10 blocks of memory for int type and Initialize all that to ZERO,

    int *ptr = (int *) calloc(10, (sizeof(int)));
    

    Similarity:

    Both malloc() and calloc() will return void* by default if they are not type casted .!

    0 讨论(0)
  • 2020-11-22 04:05

    malloc() and calloc() are functions from the C standard library that allow dynamic memory allocation, meaning that they both allow memory allocation during runtime.

    Their prototypes are as follows:

    void *malloc( size_t n);
    void *calloc( size_t n, size_t t)
    

    There are mainly two differences between the two:

    • Behavior: malloc() allocates a memory block, without initializing it, and reading the contents from this block will result in garbage values. calloc(), on the other hand, allocates a memory block and initializes it to zeros, and obviously reading the content of this block will result in zeros.

    • Syntax: malloc() takes 1 argument (the size to be allocated), and calloc() takes two arguments (number of blocks to be allocated and size of each block).

    The return value from both is a pointer to the allocated block of memory, if successful. Otherwise, NULL will be returned indicating the memory allocation failure.

    Example:

    int *arr;
    
    // allocate memory for 10 integers with garbage values
    arr = (int *)malloc(10 * sizeof(int)); 
    
    // allocate memory for 10 integers and sets all of them to 0
    arr = (int *)calloc(10, sizeof(int));
    

    The same functionality as calloc() can be achieved using malloc() and memset():

    // allocate memory for 10 integers with garbage values   
    arr= (int *)malloc(10 * sizeof(int));
    // set all of them to 0
    memset(arr, 0, 10 * sizeof(int)); 
    

    Note that malloc() is preferably used over calloc() since it's faster. If zero-initializing the values is wanted, use calloc() instead.

    0 讨论(0)
  • 2020-11-22 04:07

    The documentation makes the calloc look like malloc, which just does zero-initialize the memory; this is not the primary difference! The idea of calloc is to abstract copy-on-write semantics for memory allocation. When you allocate memory with calloc it all maps to same physical page which is initialized to zero. When any of the pages of the allocated memory is written into a physical page is allocated. This is often used to make HUGE hash tables, for example since the parts of hash which are empty aren't backed by any extra memory (pages); they happily point to the single zero-initialized page, which can be even shared between processes.

    Any write to virtual address is mapped to a page, if that page is the zero-page, another physical page is allocated, the zero page is copied there and the control flow is returned to the client process. This works same way memory mapped files, virtual memory, etc. work.. it uses paging.

    Here is one optimization story about the topic: http://blogs.fau.de/hager/2007/05/08/benchmarking-fun-with-calloc-and-zero-pages/

    0 讨论(0)
  • 2020-11-22 04:07

    The calloc() function that is declared in the <stdlib.h> header offers a couple of advantages over the malloc() function.

    1. It allocates memory as a number of elements of a given size, and
    2. It initializes the memory that is allocated so that all bits are zero.
    0 讨论(0)
  • 2020-11-22 04:09

    One often-overlooked advantage of calloc is that (conformant implementations of) it will help protect you against integer overflow vulnerabilities. Compare:

    size_t count = get_int32(file);
    struct foo *bar = malloc(count * sizeof *bar);
    

    vs.

    size_t count = get_int32(file);
    struct foo *bar = calloc(count, sizeof *bar);
    

    The former could result in a tiny allocation and subsequent buffer overflows, if count is greater than SIZE_MAX/sizeof *bar. The latter will automatically fail in this case since an object that large cannot be created.

    Of course you may have to be on the lookout for non-conformant implementations which simply ignore the possibility of overflow... If this is a concern on platforms you target, you'll have to do a manual test for overflow anyway.

    0 讨论(0)
  • 2020-11-22 04:12

    A difference not yet mentioned: size limit

    void *malloc(size_t size) can only allocate up to SIZE_MAX.

    void *calloc(size_t nmemb, size_t size); can allocate up about SIZE_MAX*SIZE_MAX.

    This ability is not often used in many platforms with linear addressing. Such systems limit calloc() with nmemb * size <= SIZE_MAX.

    Consider a type of 512 bytes called disk_sector and code wants to use lots of sectors. Here, code can only use up to SIZE_MAX/sizeof disk_sector sectors.

    size_t count = SIZE_MAX/sizeof disk_sector;
    disk_sector *p = malloc(count * sizeof *p);
    

    Consider the following which allows an even larger allocation.

    size_t count = something_in_the_range(SIZE_MAX/sizeof disk_sector + 1, SIZE_MAX)
    disk_sector *p = calloc(count, sizeof *p);
    

    Now if such a system can supply such a large allocation is another matter. Most today will not. Yet it has occurred for many years when SIZE_MAX was 65535. Given Moore's law, suspect this will be occurring about 2030 with certain memory models with SIZE_MAX == 4294967295 and memory pools in the 100 of GBytes.

    0 讨论(0)
提交回复
热议问题