Star Gazers
Published in

Star Gazers

C: Memory Allocation 🗺️


I am currently on week 4 for CS50, and we have been looking at Memory in C.

In this blog, I’ll be explaining what I have learnt about memory allocation in CS50 and from researching elsewhere. 🔬

Dynamic Memory Allocation 🔍

Now when it comes to memory allocation, we have dynamic and static allocation. An example of static allocation would be assigning a maximum length value to an array. Take this example below:

// An array of integers with a maximum length of 20.
int array[20] = {...}

You could probably already see an issue with this type of memory allocation! If the number of elements were to be above 20 during compile-time, our programme would break.

Three Different Pools of Memory 🌊

Static — Static memory endures throughout the entire life of the program, and is usually used to store global variables, or variables created with the static clause such as static int example;.

Stack — Stack stores variables used on the inside of a function (including the main() function). Every time a function declares a new variable it is “pushed” onto the stack. As soon as the function executes and exits the data is no longer stored and removed.

Heap — Heap is a large pool of memory that can be used dynamically, AKA “free store”. This is memory that is not automatically managed but has to be explicitly allocated and discarded. Therefore, memory will endure for the programs lifetime until you decide to remove it.

Malloc ➡️

Sounds like a scary alien from a neighbouring planet, right? 👽

Please say you get the reference… 👀

Don’t worry, “Memory allocation” or malloc is a method in C that is used to dynamically allocate a single large block of memory with a specified size. Here is an example:

// The pointer 'ptr' holds the address of the first byte in the allocated memory.
// An integer size is 4 bytes so therefore below will allocate 400 bytes of memory.
ptr = (int*) malloc(100 * sizeof(int));

Calloc ⬅️

The extra-terrestrials from another galaxy 🌌!?

The purpose of calloc() is to allocate a specified number of blocks of memory of the specified type and this differs from malloc() as it initialises each block with a default value of 0. Let’s take a look at the example below:

// With calloc() we are allocating distant space in memory for 20 elements each with the size of the int.ptr = (int*) calloc(20, sizeof(int));

Realloc 🔁

What if we want to reallocate memory to an existing allocated memory block? Then we use the realloc() method which dynamically achieves this and also preserves the content of the memory area.

// Here ptr is reallocated to 'newSize', being its new size.ptr = realloc(ptr, newSize);

Set Me Free 🕊

The free() method is used to dynamically ‘de-allocate’ memory. The memory allocated using malloc() and calloc() is not de-allocated and so we use free() whenever the dynamic memory allocation takes place. The example below is relating to the pointer we have used earlier:


These three methods can all be used in the same function if we are allocating memory, reallocating and then freeing up that memory.

I hope you were able to learn the basics of memory allocation in C, and are able to successfully implement it in your application!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store