Dynamic Memory Allocation Pointers in C Programming

In above all cases, pointer variables are not initialized when it is declared. It is in latter steps made to point to some variable and hence it is initialized. But if a pointer is not initialized, then it may point to any other memory location in the system. Pointer cannot point any of memory address because operating system may be using those memory locations and if a pointer points to it, then it may crash the system. Hence it is very much essential to allocate some free memory to pointers when they are created. But at the point of writing the code, we may not know which memory is free and can be allocated to pointers. Hence we need to allocate memory dynamically to the pointers – while executing the code. In addition, we may not know in advance how much memory we need for a pointer. It will be decided while executing the program and hence allocating memory dynamically will greatly help in utilizing the required memory.

An inbuilt function malloc is used to dynamically assign memory to pointers. This function is available in stdlib.h header file. The memory needed for the pointer is given as argument to this function and malloc allocates that much memory block to the pointer variable. It then returns the pointer to the block of memory that is allocated to it.

int *intPtr = malloc (4); // this will allocate 4 bytes of memory to intPtr

But we cannot always allocate constant number of memory. The memory blocks will vary across different datatypes. Suppose if the pointer is integer, then it would be 4 bytes, if it is a character, then it would be 1byte. Rather than deciding these memory blocks while coding, if we allow the compiler to decide how much memory blocks are required for the pointer, then it will have the real use of dynamic memory allocation. Hence we need to get the size of different datatypes at runtime. We can get this by using a function sizeof. This function will take an argument and it will return the size of the argument.

int *intPtr = malloc (sizeof (int)); // this will allocate memory to size of integer datatype.

Sometimes size of datatypes varies from systems to system. In some systems, integer has 4 bytes while in some systems it is only 2 bytes. In such case if we say malloc (2) and run it in other system with 4 bytes, then it will not work. Hence allowing compiler to decide the size of datatypes that we are using will make the code work efficiently.

We can similarly allocate memory to float, character,  arrays, structures, functions etc.

float *intPtr = malloc (sizeof (float)); // this will allocate memory to size of float datatype.
char *charPtr = malloc (sizeof (char)); // this will allocate memory to size of character datatype.
struct structExample *structPtr = malloc (sizeof (structExample)); // this will allocate memory to size of structure datatype.

When sizeof (structure_variable) is used to allocate memory to the pointer, then it will be the sum of individual size of each element in it. Suppose we have structure as below. Then its size would be sizeof (int) + sizeof (float) +sizeof (char).

struct structExample {
	int intX;
	float fltY;
	char chrZ;
};

We can even allocate memory after we have declared a pointer variable and somewhere else in the code but before using it in the code. We need not always allocate memory when we are declaring it.

intPtr = malloc (sizeof (int)); // this will allocate memory to size of integer datatype.

What does below memory allocation infer ?

int *intPtr = malloc (sizeof (*intPtr));

It is assigning the size of same pointer to it. But how much memory it will assign? Since it is assigning the size of *intPtr, where *intPtr is pointing to integer variable, it will allocate the size of integer to it.

char *chrPtr = malloc (sizeof (*chrPtr)); // this will allocate memory to size of character datatype.

Once memory is allocated the pointer variable, no other variables or programs can use them. Hence it is very much necessary to free those memories once it has been used. Otherwise programs will run out of memory and programs will hang.

free (pointer_variable);
free (intPtr);
Translate »