Let us continue with our memory management topic in C.
Stack Memory
For completeness, it is worth mentioning stack memory. Stack memory is a type of dynamic memory which is reserved for variables that are declared inside functions. Variables declared inside a function use stack memory rather than static memory.
When a function is called, stack memory is allocated for the variables in the function. When the function returns the stack memory is freed.
It is good to be aware of stack memory to be able to handle the memory usage of nested function calls and recursion. Recursion that repeats itself too many times may take up too much stack memory. When that happens it is called a stack overflow.
Access Dynamic Memory
Dynamic memory behaves like an array, with its data type specified by the type of the pointer. As with arrays, to access an element in dynamic memory, refer to its index number:
ptr[0] = 12;
You can also dereference the pointer to access the first element:
*ptr = 12;
Example
Read from and write to dynamic memory:
// Allocate memory
int ptr; ptr = calloc(4, sizeof(ptr));
// Write to the memory
*ptr = 2;
ptr[1] = 4;
ptr[2] = 6;
// Read from the memory
printf(“%d\n”, *ptr);
printf(“%d %d %d”, ptr[1], ptr[2], ptr[3]);
Note
Dynamic memory does not have its own data type, it is just a sequence of bytes. The data in the memory can be interpreted as a type based on the data type of the pointer.
In this example a pointer to four bytes can be interpreted as one int value (4 bytes) or as an array of 4 char values (1 byte each).
Example
int *ptr1 = malloc(4);
char *ptr2 = (char*) ptr1;
ptr1[0] = 1684234849;
printf(“%d is %c %c %c %c”, *ptr1, ptr2[0], ptr2[1], ptr2[2], ptr2[3]);
Reallocate Memory
If the amount of memory you reserved is not enough, you can reallocate it to make it larger. Reallocating reserves a different (usually larger) amount of memory while keeping the data that was stored in it. You can change the size of allocated memory with the realloc() function.
The realloc() function takes two parameters:
int *ptr2 = realloc(ptr1, size);
- The first parameter is a pointer to the memory that is being resized.
- The second parameter specifies the new size of allocated memory, measured in bytes.
The realloc() function tries to resize the memory at ptr1 and return the same memory address. If it cannot resize the memory at the current address then it will allocate memory at a different address and return the new address instead.
Note
When realloc() returns a different memory address, the memory at the original address is no longer reserved and it is not safe to use. When the reallocation is done it is good to assign the new pointer to the previous variable so that the old pointer cannot be used accidentally.
Example
Increase the size of allocated memory:
int *ptr1, *ptr2, size;
// Allocate memory for four integers
size = 4 * sizeof(*ptr1);
ptr1 = malloc(size);
printf(“%d bytes allocated at address %p \n”, size, ptr1);
// Resize the memory to hold six integers
size = 6 * sizeof(*ptr1);
ptr2 = realloc(ptr1, size);
printf(“%d bytes reallocated at address %p \n”, size, ptr2);
NULL Pointer & Error Checking
The realloc() function returns a NULL pointer if it is not able to allocate more memory. This is very unlikely, but it is worth keeping in mind when you need your code to be failproof.
The following example checks whether realloc() is able to resize the memory or not, by checking for a NULL pointer:
Example
Check for a NULL pointer:
int *ptr1, *ptr2;
// Allocate memory
ptr1 = malloc(4);
// Attempt to resize the memory
ptr2 = realloc(ptr1, 8);
// Check whether realloc is able to resize the memory or not
if (ptr2 == NULL) {
// If reallocation fails
printf(“Failed. Unable to resize memory”);
} else {
// If reallocation is sucessful
printf(“Success. 8 bytes reallocated at address %p \n”, ptr2);
ptr1 = ptr2; // Update ptr1 to point to the newly allocated memory
}
Note
- You should always include error checking (if pointer == NULL) when allocating memory.
- You should also always free, or release, allocated memory when you are done using it. This is important to make sure that your program behaves as expected, but it will also make it more maintainable and efficient.
To free memory, simply use the free() function:
Example
Free allocated memory:
// Free allocated memory
free(ptr1);



Leave a Reply