CMemory Management Example
Real-Life Memory Management Example
To demonstrate a practical example ofdynamic memory, we created a program that can make a list of any length.
Regular arrays in C have a fixed length and cannot be changed, but with dynamic memory we can create a list as long as we like:
Example
int *data; // Points to the memory where the list items are stored
int numItems; // Indicates how many items are currently in the list
int size; // Indicates how many items fit in the allocated memory
};
void addToList(struct list *myList, int item);
int main() {
struct list myList;
int amount;
int i, j;
// Create a list and start with enough space for 10 items
myList.numItems = 0;
myList.size = 10;
myList.data = malloc(myList.size * sizeof(int));
// Find out if memory allocation was successful
if (myList.data == NULL) {
printf("Memory allocation failed");
return 1; // Exit the program with an error code
}
// Add any number of items to the list specified by the amount variable
amount = 44;
for (i = 0; i < amount; i++) {
addToList(&myList, i + 1);
}
// Display the contents of the list
for (j = 0; j < myList.numItems; j++) {
printf("%d ", myList.data[j]);
}
// Free the memory when it is no longer needed
free(myList.data);
myList.data = NULL;
return 0;
}
// This function adds an item to a list
void addToList(struct list *myList, int item) {
// If the list is full then resize the memory to fit 10 more items
if (myList->numItems == myList->size) {
int newSize = myList->size + 10;
// Use a temporary pointer so we don't lose the original on failure
int *tmp = realloc(myList->data, newSize * sizeof(int));
if (tmp == NULL) {
printf("Memory resize failed\n");
return; // Leave the list unchanged
}
// Only update fields after a successful reallocation
myList->data = tmp;
myList->size = newSize;
}
// Add the item to the end of the list
myList->data[myList->numItems] = item;
myList->numItems++;
}
Pointers to structures: This example has a pointer to the structuremyList. Because we are using apointer to the structure instead of the structure itself, we use the arrow syntax (->) to access the structure's members.
Example explained
This example has three parts:
- A structure
myListthat contains a list's data - The
main()function with the program in it. - A function
addToList()which adds an item to the list
ThemyList structure
ThemyList structure contains all of the information about the list, including its contents. It has three members:
data- A pointer to the dynamic memory which contains the contents of the listnumItems- Indicates the number of items that list hassize- Indicates how many items can fit in the allocated memory
We use a structure so that we can easily pass all of this information into a function.
Themain() function
Themain() function starts by initializing the list with space for 10 items:
myList.numItems = 0;
myList.size = 10;
myList.data = malloc(myList.size * sizeof(int));
myList.numItems is set to 0 because the list starts off empty.
myList.size keeps track of how much memory is reserved. We set it to 10 because we will reserve enough memory for 10 items.
We then allocate the memory and store a pointer to it inmyList.data.
Then we include error checking to find out if memory allocation was successful:
if (myList.data == NULL) {
printf("Memory allocation failed");
return 1; // Exit the program with an error code
}
If everything is fine, a loop adds 44 items to the list using theaddToList() function:
amount = 44;
for (i = 0; i < amount; i++) {
addToList(&myList, i + 1);
}
In the code above,&myList is a pointer to the list andi + 1 is a number that we want to add to the list. We chosei + 1 so that the list would start at 1 instead of 0. You can choose any number to add to the list.
After all of the items have been added to the list, the next loop prints the contents of the list.
for (j = 0; j < myList.numItems; j++) {
printf("%d ", myList.data[j]);
}
When we finish printing the list we free the memory to prevent memory leaks.
free(myList.data);
myList.data = NULL;
TheaddToList() function
OuraddToList() function adds an item to the list. It takes two parameters:
void addToList(struct list *myList, int item)- A pointer to the list.
- The value to be added to the list.
The function first checks if the list is full by comparing the number of items in the list to the size (capacity). If the list is full, it tries to grow the memory to fit 10 more items. We use atemporary pointer withrealloc so we don't lose the original block if the resize fails. We only updatedata andsize after a successful resize:
// If the list is full then resize the memory to fit 10 more itemsif (myList->numItems == myList->size) { int newSize = myList->size + 10; // Use a temp pointer so we don't lose the original on failure int *tmp = realloc(myList->data, newSize * sizeof(int)); if (tmp == NULL) { printf("Memory resize failed\n"); return; // Leave the list unchanged } // Only update fields after a successful reallocation myList->data = tmp; myList->size = newSize;} Finally, the function adds the item to the end of the list. The index atmyList->numItems is always at the end of the list because it increases by 1 each time a new item is added.:
// Add the item to the end of the listmyList->data[myList->numItems] = item;myList->numItems++;Why do we reserve 10 items at a time?
Optimizing is a balancing act between memory and performance. Even though we may be allocating some memory that we are not using, reallocating memory too frequently can be inefficient. There is a balance between allocating too much memory and allocating memory too frequently.
We chose the number 10 for this example, but it depends on how much data you expect and how often it changes. For example, if we know beforehand that we are going to have exactly 44 items then we can allocate memory for exactly 44 items and only allocate it once.
Complete stdlib Reference
For a complete reference of memory management functions and other functions found in the standard library, go to ourC <stdlib.h> Library Reference.

