How to Allocate Memory in C

C is a middle level computer programming language that is developed in 1973 by Dennis Ritchie at the Bell Labs. C was initially designed for implementing system software but now it is also widely used to develop portable application software. C is one of the most talked about programming languages of all times. There are very less computer architectures for which a C compiler is not there. C is responsible for affecting many other popular programming languages, most notably C++.  The most important function of a programming language is to manage memory and the objects that you store in memory. If you also look out for how to allocate memory in C, you are surely at the right place. This paper helps you to know about pointers and memory allocations in C.

There are three distinct ways to allocate memory for objects in C:

Static memory allocation:

At compile time, the required space is allocated for the object in the binary. These objects will exist as long as the binary exists and these objects are loaded into memory.

Automatic memory allocation:

You can store the temporary objects on the stack. This space is then automatically emptied and reusable after the block in which they are declared is exited.

Dynamic memory allocation:

You can request the blocks of memory of arbitrary size at the run-time using library functions. The library functions can be malloc() and it is from a region of memory called the heap. These blocks remain until they get free.

All these three approaches can be appropriate depending upon different situations. For example, static memory allocation does not come with any allocation overhead. The automatic allocation may have a small amount of overhead, and dynamic memory allocation can have a larger overhead for both allocation and deallocation. On the other hand, stack space is limited and transient than either static memory. Dynamic memory allocation allows allocation of those objects for which size is known only at run-time. Most C programs take the advantage of all the three approaches.

Usually, automatic or static allocation is preferred because the compiler manages the storage issues. This removes any possibility of error of manually allocating and releasing storage for the programmer. However, different data structures can increase in size at runtime. Since, static allocations need to have a fixed size at compile-time; you can use dynamic allocation in such situations.

You can initialize automatically and dynamically allocated objects only if you have explicitly specified an initial value. Otherwise, they initially have indeterminate values. Whenever the program tries to access an uninitialized value, you cannot define the results. Many modern compilers try to detect this problem, but both false positives and false negatives occur.

Another issue is that you need to manually synchronize the heap memory allocation with its actual usage in any program. This helps to reuse it as much as possible. There can be two scenarios for the only pointer to a heap memory allocation. The one is that when it goes out of scope or its value overwritten before free() has been called, then you cannot recover the memory for later reuse. This memory is essentially lost to the program. This is a phenomenon known as a memory leak. On the contrary, it is also possible to release memory too soon and continue to access it. However, as the allocation system can re-allocate or can use the freed memory, unusual behavior may occur. Typically, the symptoms will be shown in a portion of the program than the actual error. This can make it difficult to track down the actual problem. You can find such issues that have automatic garbage collection.

You can easily allocate memory at run time using malloc(), calloc(), or other allocation functions. This approach lets you to postpone the decision on the size of the memory block need to store an array. This can happen until run time. It may also permit using a section of memory for the storage of an array of integers at one point in time. The memory that you no longer need, can be emptied for other uses such as the storage of an array of structures.

When you allocate the memory, the allocating function like malloc(), calloc(), etc. replies with a pointer. This pointer depends on the type of compiler you are using. It can be an older K&R compiler or the ANSI compiler. The older compiler returns the pointer of char data type, whereas the ANSI compiler returns void.

If you use an older compiler and you are looking to allocate memory for an array of integers, you need to cast the char pointer returned to an integer pointer. For example, if you want to allocate space for 10 integers, you might write like:

int *ipt;

ipt = (int *)malloc(10 * sizeof(int));

if (ipt == NULL)

{ERROR ROUTINE GOES HERE}

With an ANSI compliant compiler, malloc() returns a void pointer. As you can assign variable of any object type to the void pointer, you do not require any (int *) cast as shown above. You can determine the array dimension at run time and it is also not needed at compile time. You can read the 10 above as a variable from a data file or keyboard, or calculated based on some need at the run time.

As there is some sort of equivalence between array and pointer notation, once ipt has been assigned as above, you can use the array notation. For example, you could write:

int k;

for (k = 0; k < 10; k++)

ipt[k] = 2;

to set the values of every element to 2.

Even if you have a reasonably good understanding of pointers and arrays, you can stumble at first in the dynamic allocation of multi-dimensional arrays. In general, you can access elements of such arrays using array notation, not pointer notation, wherever possible. You can easily allocate memory in C after reading this article. Now, I believe that you have enough information on how to allocate memory in C.


Related Content:

  1. How to Allocate System Memory
  2. How to Allocate Virtual Memory
  3. How to Allocate Bandwidth
  4. How to Prevent Memory Loss
  5. How to Improve Memory
. Tags: .

Leave a Reply