Name

malloc, calloc, realloc, free, reallocf
- Allocate and free dynamic memory

Library

libc.lib

Synopsis

  #include <stdlib.h>
  void * malloc (size_t size);
  void * calloc (size_t number, size_t size);
  void * realloc (void *ptr, size_t size);
  void * reallocf (void *ptr, size_t size);
  void free (void *ptr);

Return values

The malloc and calloc functions return a pointer to the allocated memory if successful; otherwise a NULL pointer is returned and errno is set to ENOMEM.

The realloc and reallocf functions return a pointer, possibly identical to ptr, to the allocated memory if successful; otherwise a NULL pointer is returned, and errno is set to ENOMEM if the error was the result of an allocation failure. The realloc function always leaves the original buffer intact when an error occurs, whereas reallocf deallocates it in this case.

The free function returns no value.


Detailed description

The malloc function allocates size bytes of memory. The allocated space is suitably aligned (after possible pointer coercion) for storage of any type of object. If the space is at least pagesize bytes in length (see getpagesize the returned memory will be page boundary aligned as well. If malloc fails, a NULL pointer is returned.

Note that malloc does NOT normally initialize the returned memory to zero bytes.

The calloc function allocates space for number objects, each size bytes in length. The result is identical to calling malloc with an argument of "number * size", with the exception that the allocated memory is explicitly initialized to zero bytes.

The realloc function changes the size of the previously allocated memory referenced by ptr to size bytes. The contents of the memory are unchanged up to the lesser of the new and old sizes. If the new size is larger, the value of the newly allocated portion of the memory is undefined. If the requested memory cannot be allocated, NULL is returned and the memory referenced by ptr is valid and unchanged. If memory can be allocated, the memory referenced by ptr is freed and a pointer to the newly allocated memory is returned. Note that realloc and reallocf may move the memory allocation resulting in a different return value than ptr. If ptr is NULL, the realloc function behaves identically to malloc for the specified size.

The reallocf function is identical to the realloc function, except that it will free the passed pointer when the requested memory cannot be allocated. This is a
specific API designed to ease the problems with traditional coding styles for realloc causing memory leaks in libraries.

The free function causes the allocated memory referenced by ptr to be made available for future allocations. If ptr is NULL, no action occurs.


Examples

#include <stdlib.h>    //malloc
#include <stdio.h>    //printf
  
int main( void )
{
   /* allocate memory */
   char  *pc = (char *)malloc( sizeof(char) * 3);
   
   if( pc == NULL )
      printf( "memory insufficient\n" );
   else
   {
      printf( "memory allocated\n" );
      free( pc );
      printf( "memory freed\n" );
   }
  
   return 0;
}

         

Output

memory allocated
memory freed

         

#include <stdio.h> //printf
#include <stdlib.h> //calloc
   
int main( void )
{
   int  *pint = (int *)calloc(2, sizeof (int) * 2);
   if( pint != NULL )
      printf( "allocated 2 blocks of memory, each of size -
               twice the size of an integer\n" );
   else
      printf( "can’t allocate memory\n" );
   free( pint );
  
   return 0;
}

         

Output

allocated 2 blocks of memory, each of size - 2 integers

         

#include <stdio.h> //printf
#include <stdlib.h> //realloc
 
int main( void )
{
 int  *pint = (int *)calloc(2, sizeof (int) * 2);
 if (pint == NULL)
  {
  printf("calloc failed to allocate memory\n");
  return -1;
  }
 else
  {
  printf("calloc allocated memory: 128 bytes\n");
  }
 
 pint = (int *)realloc(pint, (sizeof (int) * 6) );
 if (pint == NULL)
  {
  printf("realloc failed to allocate memory\n");
  return -1;
  }
 else
  {
  printf("realloc allocated memory: 192 bytes\n");
  }
  
  free( pint );
  return 0;
}

         

Output

calloc allocated memory: 128 bytes
realloc allocated memory: 192 bytes

         

See also

brk, mmap, getpagesize

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top