Standard C Memory

C++ Reference

calloc
Syntax:
  #include <stdlib.h>
  void* calloc( size_t num, size_t size );

The calloc() function returns a pointer to space for an array of num objects, each of size size. The newly allocated memory is initialized to zero.

calloc() returns NULL if there is an error.

Related topics:

free
Syntax:
  #include <stdlib.h>
  void free( void* ptr );

The free() function deallocates the space pointed to by ptr, freeing it up for future use. ptr must have been used in a previous call to malloc(), calloc(), or realloc(). An example:

   typedef struct data_type {
     int age;
     char name[20];
   } data;              

   data *willy;
   willy = (data*) malloc( sizeof(*willy) );
   ...
   free( willy );               
Related topics:

malloc
Syntax:
  #include <stdlib.h>
  void *malloc( size_t size );

The function malloc() returns a pointer to a chunk of memory of size size, or NULL if there is an error. The memory pointed to will be on the heap, not the stack, so make sure to free it when you are done with it. An example:

   typedef struct data_type {
     int age;
     char name[20];
   } data;              

   data *bob;
   bob = (data*) malloc( sizeof(data) );
   if( bob != NULL ) {
     bob->age = 22;
     strcpy( bob->name, "Robert" );
     printf( "%s is %d years old\n", bob->name, bob->age );
   }
   free( bob );         
Related topics:

realloc
Syntax:
  #include <stdlib.h>
  void *realloc( void *ptr, size_t size );

The realloc() function changes the size of the object pointed to by ptr to the given size. size can be any size, larger or smaller than the original. The return value is a pointer to the new space, or NULL if there is an error.

Related topics: