C Programming/Memory management

In C, you have already considered creating variables for use in the program. You have created some arrays for use, but you may have already noticed some limitations:
 * the size of the array must be known beforehand
 * the size of the array cannot be changed in the duration of your program

Dynamic memory allocation in C is a way of circumventing these problems.

The function
The standard C function  is the means of implementing dynamic memory allocation. It is defined in stdlib.h or malloc.h, depending on what operating system you may be using. Malloc.h contains only the definitions for the memory allocation functions and not the rest of the other functions defined in stdlib.h. Usually you will not need to be so specific in your program, and if both are supported, you should use , since that is ANSI C, and what we will use here.

The corresponding call to release allocated memory back to the operating system is.

When dynamically allocated memory is no longer needed,  should be called to release it back to the memory pool. Overwriting a pointer that points to dynamically allocated memory can result in that data becoming inaccessible. If this happens frequently, eventually the operating system will no longer be able to allocate more memory for the process. Once the process exits, the operating system is able to free all dynamically allocated memory associated with the process.

Let's look at how dynamic memory allocation can be used for arrays.

Normally when we wish to create an array we use a declaration such as

Recall  can be considered a pointer which we use as an array. We specify the length of this array is 10 s. After , nine other integers have space to be stored consecutively.

Sometimes it is not known at the time the program is written how much memory will be needed for some data; for example, when it depends upon user input. In this case we would want to dynamically allocate required memory after the program has started executing. To do this we only need to declare a pointer, and invoke  when we wish to make space for the  elements in our array, or, we can tell   to make space when we first initialize the array. Either way is acceptable and useful.

We also need to know how much an int takes up in memory in order to make room for it; fortunately this is not difficult, we can use C's builtin  operator. For example, if  yields 4, then one   takes up 4 bytes. Naturally,  is how much memory we need for 2  s, and so on.

So how do we  an array of ten  s like before? If we wish to declare and make room in one hit, we can simply say We only need to declare the pointer;  gives us some space to store the 10  s, and returns the pointer to the first element, which is assigned to that pointer.

Important note!  does not initialize the array; this means that the array may contain random or unexpected values! Like creating arrays without dynamic allocation, the programmer must initialize the array with sensible values before using it. Make sure you do so, too. (See later the function  for a simple method.)

It is not necessary to immediately call  after declaring a pointer for the allocated memory. Often a number of statements exist between the declaration and the call to, as follows:

A more practical example of dynamic memory allocation would be the following:"Given an array of 10 integers, remove all duplicate elements from the array, and create a new array without duplicate elements (a set)."A simple algorithm to remove duplicate elements: Because the length of our new array depends on the input, it must be dynamically allocated: The above array will currently contain unexpected values, so we must use  to set our dynamically allocated memory block to the new values:

Some security researchers recommend always using calloc(x,y) rather than malloc(x*y), for 2 reasons:
 * Many implementations of calloc carefully check the x and y arguments and return NULL if "x*y" could overflow. Using malloc(x*y), the multiplication "x*y" can overflow to 0 or some other too-small number, usually leading to buffer overflow.
 * calloc ensures that the buffer is completely empty of sensitive information, avoiding some kinds of security bugs (but, unfortunately, this would not have prevented the Heartbleed bug).

Error checking
When we want to use, we have to be mindful that the pool of memory available to the programmer is finite. Even if a modern PC will have at least an entire gigabyte of memory, it is still possible and conceivable to run out of it! In this case,  will return. In order to stop the program crashing from having no more memory to use, one should always check that malloc has not returned  before attempting to use the memory; we can do this by

Of course, suddenly quitting as in the above example is not always appropriate, and depends on the problem you are trying to solve and the architecture you are programming for. For example, if the program is a small, non critical application that's running on a desktop quitting may be appropriate. However if the program is some type of editor running on a desktop, you may want to give the operator the option of saving their tediously entered information instead of just exiting the program. A memory allocation failure in an embedded processor, such as might be in a washing machine, could cause an automatic reset of the machine. For this reason, many embedded systems designers avoid dynamic memory allocation altogether.

The function
The  function allocates space for an array of items and initializes the memory to zeros. The call  allocates   objects, each of whose size is sufficient to contain an instance of the structure. The space is initialized to all bits zero. The function returns either a pointer to the allocated memory or, if the allocation fails,.

The function
The  function changes the size of the object pointed to by   to the size specified by. The contents of the object shall be 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 object is indeterminate. If  is a null pointer, the   function behaves like the   function for the specified size. Otherwise, if  does not match a pointer earlier returned by the ,  , or   function, or if the space has been deallocated by a call to the   or   function, the behavior is undefined. If the space cannot be allocated, the object pointed to by  is unchanged. If  is zero and   is not a null pointer, the object pointed to is freed. The  function returns either a null pointer or a pointer to the possibly moved allocated object.

The function
Memory that has been allocated using,  , or   must be released back to the system memory pool once it is no longer needed. This is done to avoid perpetually allocating more and more memory, which could result in an eventual memory allocation failure. Memory that is not released with  is however released when the current program terminates on most operating systems. Calls to  are as in the following example.

free with recursive data structures
It should be noted that  is neither intelligent nor recursive. The following code that depends on the recursive application of free to the internal variables of a struct does not work.

The statement " " will not free, causing a memory leak. The correct way is to define a function that frees every node in the data structure:

Because C does not have a garbage collector, C programmers are responsible for making sure there is a  exactly once for each time there is a. If a tree has been allocated one node at a time, then it needs to be freed one node at a time.

Don't free undefined pointers
Furthermore, using  when the pointer in question was never allocated in the first place often crashes or leads to mysterious bugs further along.

To avoid this problem, always initialize pointers when they are declared. Either use  at the point they are declared (as in most examples in this chapter), or set them to   when they are declared (as in the "delayed allocation" example in this chapter).

Write constructor/destructor functions
One way to get memory initialization and destruction right is to imitate object-oriented programming. In this paradigm, objects are constructed after raw memory is allocated for them, live their lives, and when it is time for them to be destructed, a special function called a destructor destroys the object's innards before the object itself is destroyed.

For example: