Post: Memory Allocation in C Language
04-24-2013, 08:27 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Hello everyone,
First of all visit my forum for all Tuts You must login or register to view this content.

When an ELF executable is executed, a process is created and its process image is create in the RAM. However, it is here in the process image in RAM, all the variables are assigned memory. Sometimes memory is allocated statically i.e. defined how much memory at the compile time, and at times it has to be allocated dynamically. Where, it is specified at run time the amount of memory needed. There are 2 times of memory allocations in C Language they are

1.Static Memory Allocation
2.Dynamic Memory Allocation



Static Memory Allocation

First of all, please note, static memory allocation is not related to the ‘static’ variables in C programing.
Look at the following statement of a program:

Originally posted by another user
int var=30;


It is an initialized variable which is of datatype ‘int’ and has been allocated memory of the size of ‘int’ (lets say 4 bytes, which is a standard in Linux). Such kind of memory allocations are called static memory allocations. Note, at compile time we know here that ‘sizeof(int)’ amount of memory will be allocated. Here are some more examples of static memory allocation

Originally posted by another user
char c;
float fvar;
int array[12];


This also explains why arrays take only constants as their sizes.

Worth mentioning, all such static memory allocations take place in the stack of the process image with certain exceptions like uninitialised global variables, const string pointers etc.

Dynamic Memory Allocation

In several cases, we might not know how much memory do we need at the compile-time. Its can only be specified at the run-time based on the value of a certain variable. For example, in the case of linked lists, where a node can be added/deleted at runtime, or matrices of varying rows/columns. For such scenarios, we have dynamic memory allocations. C provides following methods to allocate memory. Dynamic memory allocation always allocates memory in the heap of the process image.
Originally posted by another user

void *malloc(size_t size);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);


1. ‘malloc()’ is used for allocating a specific amount (in bytes) of memory. It returns a pointer to the memory allocated. However, the returned pointer is a void pointer which needs to be type-casted for regular use. It does not initialise the allocated memory. It returns a NULL pointer in case the allocations fails due to any reason. So, one can check for success/failure of this method by checking if the returned pointer is NULL or not. For more such details regarding malloc method, please refer its man page.

Originally posted by another user
int *ptr = NULL;
int amt = 30;
ptr = (int*) malloc ( amt * sizeof(int));


2. ‘calloc()’ is used for allocating a specific amount of a particle datatype of a memory. Hence, it takes two arguments as one can see in the syntax provided above. ‘calloc()’ also returns the pointer to the allocated chunk of memory, and it also needs the type casting. However, it attempts to initialise all the bits of the allocated memory to zero. It returns a NULL pointer in case the allocations fails due to any reason. For more details on this method, please refer to its man page.
An example usage

Originally posted by another user
int *ptr = NULL;
ptr = (int*) malloc ( amt, sizeof(int));


3. Another method for dynamic memory allocation is ‘realloc()’. The above two methods are there for fresh memory allocations. However, what if one just wants to expand/shrink an already allocated chunk of memory? For such cases we have ‘realloc()’. Depending upon the memory layout, it either just allocates the extra memory demanded or re-allocated the whole requested memory. It behaves similar to ‘malloc()’ as far as memory allocations are concerned.
An example usage

Originally posted by another user
int *newPtr = NULL;
newPtr = (int*) realloc (ptr, amt + extra);


Note, ‘realloc()’ can also do fresh allocation of memory if the input pointer is a NULL.


Freeing up

Power comes with responsibility. Since C gives you the power to use and allocated memory as per our needs, hence it is the onus of the programmer to take care of allocated and free memory. Hence, freeing up of allocated memory is very essential in C programming and avoids many vulnerabilities.

All the statically allocated memory is free-ed automatically as its scope ends. However, the dynamically allocated memory have to be free-ed by the programmer. Here is the method which is used to free memory allocated on heap.

Originally posted by another user
void free(void *ptr);

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo