The Dynamic Allocation Functions

The Dynamic Allocation Functions 

Overview

This chapter describes the C/C++ dynamic allocation functions. At their core are malloc( ) and free( ). Each time malloc( ) is called, a portion of the remaining free memory is allocated. Each time free( ) is called, memory is returned to the system. The region of free memory from which memory is allocated is called the heap. For a C program, the prototypes for the dynamic allocation functions are in <stdlib.h>. For C++, the header is <cstdlib>. For ease of discussion, this chapter will use the C header name, but references to <stdlib.h> also apply to <cstdlib>.

All C/C++ compilers will include at least these four dynamic allocation functions: calloc( ), malloc( ), free( ), realloc( ). However, your compiler will almost certainly contain several variants on these functions to accommodate various options and environmental differences. You will want to refer to your compiler’s documentation.
While C++ supports the dynamic allocation functions described here, you will typically not use them in a C++ program. The reason for this is that C++ provides dynamic allocation operators called new and delete. There are several advantages to using C++’s dynamic allocation operators. First, new automatically allocates the correct amount of memory for the type of data being allocated. Second, it returns the correct type of pointer to that memory. Third, both new and delete can be overloaded. Since new and delete have advantages over the dynamic allocation functions, their use
is recommended for C++ programs

Overview

This chapter describes the C/C++ dynamic allocation functions. At their core are malloc( ) and free( ). Each time malloc( ) is called, a portion of the remaining free memory is allocated. Each time free( ) is called, memory is returned to the system. The region of free memory from which memory is allocated is called the heap. For a C program, the prototypes for the dynamic allocation functions are in <stdlib.h>. For C++, the header is <cstdlib>. For ease of discussion, this chapter will use the C header name, but references to <stdlib.h> also apply to <cstdlib>.
All C/C++ compilers will include at least these four dynamic allocation functions: calloc( ), malloc( ), free( ), realloc( ). However, your compiler will almost certainly contain several variants on these functions to accommodate various options and environmental differences. You will want to refer to your compiler’s documentation.
While C++ supports the dynamic allocation functions described here, you will typically not use them in a C++ program. The reason for this is that C++ provides dynamic allocation operators called new and delete. There are several advantages to using C++’s dynamic allocation operators. First, new automatically allocates the correct amount of memory for the type of data being allocated. Second, it returns the correct type of pointer to that memory. Third, both new and delete can be overloaded. Since new and delete have advantages over the dynamic allocation functions, their use
is recommended for C++ programs

free

#include <stdlib.h>void free(void *ptr);
 
The free( ) function returns the memory pointed to by ptr to the heap. This makes the memory available for future allocation.
It is imperative that free( ) only be called with a pointer that was previously allocated using one of the dynamic allocation system’s functions. Attempting to free an invalid pointer will most likely destroy the memory management mechanism and possibly cause a system crash. If you pass a null pointer, free( ) performs no operation.
Related functions are calloc( ), malloc( ), and realloc( ).

malloc

#include <stdlib.h>void *malloc(size_t size);
 
The malloc( ) function returns a pointer to the first byte of a region of memory of size size that has been allocated from the heap. If there is insufficient memory in the heap to satisfy the request, malloc( ) returns a null pointer. It is always important to verify that the return value is not null before attempting to use it. Attempting to use a null pointer will usually result in a system crash.
Related functions are free( ), realloc( ), and calloc( ).


Programming Tip 
If you are writing 16-bit programs for the 8086 family of processors, then your compiler will provide additional allocation functions that accommodate the segmented memory used by these processors when operating in 16-bit mode. For example, there will be functions that allocate memory from the FAR heap (the heap that is outside the default data segment), that can allocate pointers to memory that is larger than one segment, and that free such memory.

realloc

#include <stdlib.h>void *realloc(void *ptr, size_t size);
 
The precise operation of realloc( ) is slightly different in C99 than it is in C++ and C89, although the net effect is the same. For C++ and C89, realloc( ) changes the size of the previously allocated memory pointed to by ptr to that specified by size. The value of size can be greater or less than the original. A pointer to the memory block is returned because it may be necessary for realloc( ) to move the block in order to change its size. If this occurs, the contents of the old block (up to size bytes) are copied into the new block.
For C99, the block of memory pointed to by ptr is freed and a new block is allocated. The new block contains the same contents as the original block (up to the length passed in size). A pointer to the new block is returned. It is permissible, however, for the new block and the old block to begin at the same address. That is, the pointer returned by realloc( ) might be the same as the one passed in ptr.
If ptr is null, realloc( ) simply allocates size bytes of memory and returns a pointer to it. If size is zero, the memory pointed to by ptr is freed.
If there is not enough free memory in the heap to allocate size bytes, a null pointer is returned and the original block is left unchanged.
Related functions are free( ), malloc( ), and calloc( )

Followers