“Memory Management in C – Part 2

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:

You can also dereference the pointer to access the first element:

Read from and write to dynamic memory:

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).

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:

  • 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.

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.

Increase the size of allocated memory:

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:

Check for a NULL pointer:

  • 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:

Free allocated memory:

Check Your Learning….

Blogroll


Social, Cultural u0026amp; Behavioral Issues in PHC u0026amp; Global Health
Social, Cultural u0026amp; Behavioral Issues in PHC u0026amp; Global Health
Social, Cultural and Behavioral Foundations of Primary Health Care (JHSPH)

Penny Zeller
Penny Zeller
Random thoughts from a day in the life of a wife, mom, and author


Discover more from Sophia's Tech Journey

Subscribe to get the latest posts sent to your email.

Leave a Reply

Discover more from Sophia's Tech Journey

Subscribe now to keep reading and get access to the full archive.

Continue reading