Memory Allocation
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
- Dynamic 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.
35 | 45 | 62 | 19 | 25 | 69 | 89 | 87 | 89 |
-> 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.
- Malloc()
- Free()
- Calloc()
- Realloc()
- The Malloc() Technique
- C Calloc() Technique
- C free() Technique
- C realloc() technique