Memory allocation can be defined as the process of setting aside sections of memory to be used to store instances of structures, variables, and classes. The two basic types of memory allocation are:
- Static memory allocation
It involves declaring instances of structure, classes or variables and having the operating system allocate memory for that object. The programmer can use the name used to declare the object to access that block of memory.
- Dynamic memory allocation
In dynamic memory allocation, the operating system assigns a block of memory of the right size while the program is running. This can be done either by using the malloc function or with a new operator. After the block memory has been allocated, a pointer to the block is returned.
Avail our c programming homework help if you need help with assignments related to memory allocation.
Dynamic memory allocation techniques in C
The C programming language is often referred to as a structured language. This is because it has some fixed rules and conventions that programmers must follow while coding. One of the rules in C is changing the size of an array. We can define an array as a collection of items that are stored in contiguous memory allocations.
0 1 2 3 4 5 6 7 8 -> Array indices
Array length = 9
First index = 0
Last index = 8
From the example above, the size or length of the array is nine. However, there are times when you might be required to change this length, like in the following situations:
- When we only need to enter five elements in this array. This means that the four indices remaining are just wasting memory. Thus, we need to reduce the size of the array from 9 to 5
- In another situation, we may need to add three more elements to the nine we already have. For this reason, we need to lengthen the size of the array from 9 to 12.
This procedure of changing the data structure during runtime is known as dynamic memory allocation. The c programming language has functions that can handle these tasks. The <stdlib.h> has the four library functions that facilitate dynamic memory allocation in C programming. They are:
We have discussed them in detail below:
- The Malloc() Technique
“Malloc” is the short-form of memory allocation. It is a dynamic method in C used to allocate memory (single large block) with the specified size. The C malloc() method returns a pointer of type void, which can be cast into a pointer of any form.
The syntax for the C malloc() is:
Ptr = (cast-type*) malloc (byte-size);
ptr = (Int*) malloc (100* sizeof(int));
If the size of int is 4 bytes, then the statement above will allocate 400 bytes of memory. Also, the pointer ptr will hold the address of the first byte in the allocated memory. The allocation will fail and return a NULL pointer if space is insufficient.
- C Calloc() Technique
“Calloc,” also known as contiguous allocation, is a dynamic memory allocation method in C used to assign a specified number of blocks of memory of the specified type. This method initializes each block with a default value of ‘0’.
The C Calloc() syntax is:
Ptr = (cast-type*) calloc(n, element-size);
Ptr = (float*) calloc(25, sizeof(float));
In the statement above, we have allocated contiguous space memory for 25 elements, each with the size of the float. If there is insufficient space, the allocation process will fail, and the code returns a NULL pointer.
- C free() Technique
The free() in C is a dynamic method used to de-allocate memory. After allocating memory using the malloc() and the calloc() techniques, the memory will not be de-allocated on their own. Hence, whenever dynamic memory allocation has happened, the free() must be used. This technique reduces the wastage of memory by freeing it.
The syntax for the C free() method is:
- C realloc() technique
The term “realloc” is derived from the word re-allocation. C realloc() is a method that dynamically changes the memory allocation previously allotted to an element. In simple terms, if we used the calloc or malloc function to allocate memory, but it is insufficient, the realloc() can be used to reallocate the memory dynamically.
The syntax used in realloc() method is:
ptr = realloc (ptr, newSize);
This means that ptr is re-allocated with a new size “newSize.”
Allocation will fail if space is insufficient and return a Null pointer. Avail of our help with C programming assignment if you need help with homework on any of these methods.