🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C Notes
Topic #86

C Memory Example


Real-Life Memory Management Example

To demonstrate a practical example of dynamic 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

struct list {
  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++;
}

Note: Pointers to structures: This example has a pointer to the structure myList. Because we are using a pointer 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 myList that contains a list's data
  • The main() function with the program in it.
  • A function addToList() which adds an item to the list

The myList structure

The myList 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 list
  • numItems - Indicates the number of items that list has
  • size - 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.

The main() function

The main() function starts by initializing the list with space for 10 items:

  // Create a list and start with enough 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 in myList.data.

Then we include error checking to find out if memory allocation was successful:

// 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 the addToList() function:

// 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);
}

In the code above, &myList is a pointer to the list and i + 1 is a number that we want to add to the list. We chose i + 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.

// Display 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 the memory when it is no longer needed
free(myList.data);
  myList.data = NULL;

The addToList() function

Our addToList() function adds an item to the list. It takes two parameters:

void addToList(struct list *myList, int item)
  1. A pointer to the list.
  2. 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 a temporary pointer with realloc so we don't lose the original block if the resize fails. We only update data and size after a successful resize:

// 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 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 at myList->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 list
myList->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 our C <stdlib.h> Library Reference.

Want to go beyond the notes?

Join CodingNow 2.0's C course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

C Memory Example – FAQs

Quick answers about learning C Memory Example in C.

This free note from CodingNow 2.0 explains C Memory Example in C — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C topic on CodingNow 2.0, including C Memory Example, is 100% free with no signup required.
With focused practice, most students grasp C Memory Example in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now