Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>calloc() allocate and zero memory</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 calloc()                Allocate and Zero Memory

 #include   <malloc.h>                   Required for declarations only
 #include   <stdlib.h>                   For ANSI compatibility

 void       *calloc(n,size);
 size_t n;                               Number of elements to allocate
 size_t size;                            Number of bytes in each element

    calloc() allocates storage for 'n' elements, each of 'size' bytes.
    All elements are initialized to 0.  The storage space is guaranteed
    to be correctly aligned for any data type.


    Returns:    Pointer to the allocated space, or NULL (defined in
                <stdio.h>) if the space cannot be allocated.  Use a type
                cast on the return value to get a pointer to a type other
                than void.

      Notes:    Memory allocated with calloc() should only be freed with
                free().

 Compatibility: MSC version 4.0 would accept a 'size' of 0 and then
                allocate a 0 length (header only) block in the heap.
                Version 5.0 and later simply return NULL if 'size'=0.

   -------------------------------- Example ---------------------------------

    The following statements allocate space for 50 long integers and then
    free the allocated space.

           #include <malloc.h>
           #include <stdio.h>      /* for 'printf' and NULL */

           long *memptr;

           main()
           {
               if ((memptr = (long *) calloc(50, sizeof(long))) == NULL)
                    printf("not enough room to allocate memory\n");
               else {
                     .
                   free(memptr);
               }
           }


See Also: free() halloc() hfree() malloc() realloc()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson