Day 36: Memory ‘Re’ and ‘De’ Allocation in C

I Finished reading through the article on memory allocation in C. Going over it again really helped solidify things for me. Now it’s time to get back to linked list implementations in C

TLDR;

Okay, so here are the highlights of what I did:

  • Started on the “Technical Questions” section of the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. Took some time to review how memory allocation works in C. Finished reading up on how re-allocation and de-allocation works using the realloc() and free() methods in C.

Notes from Memory Allocation

The malloc() Method

The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. It doesn’t Initialize memory at execution time so that it has initialized each block with the default garbage value initially.

ptr = (cast-type*) malloc(byte-size)

// For Example:
ptr = (*int) malloc(100 * sizeof(int))

// Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. 
// And, the pointer ptr holds the address of the first byte in the allocated memory.
// If space is insufficient, allocation fails and returns a NULL pointer.

The calloc() Method

The “calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type. It is very much similar to malloc() but has two different points and these are:

  1. It initializes each block with a default value 0.
  2. It has two parameters or arguments as compared to malloc() which only has one.
ptr = (cast-type*)calloc(n, element-size);

// For Example:
ptr = (float*) calloc(25, sizeof(float));

// This statement allocates contiguous space in memory for 
// 25 elements each with 
// the size of a float type. 
// If space is insufficient, allocation fails and returns a NULL pointer.

The free() Method

The “free” method in C is used to dynamically de-allocate memory. The memory allocated using functions malloc() and calloc() is not de-allocated on their own. Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps to reduce wastage of memory by freeing it.

free(ptr);

The realloc() Method

The “realloc” or “re-allocation” method in C is used to dynamically change the memory allocation of a previously allocated memory. If the memory previously allocated with the help of malloc or calloc is insufficient, realloc can be used to dynamically re-allocate memory. re-allocation of memory maintains the already present value and new blocks will be initialized with the default garbage value.

ptr = realloc(ptr, newSize);

// where ptr is reallocated with new size 'newSize'.
// If space is insufficient, allocation fails and returns a NULL pointer.

References:


Conclusion

That’s all for today. This is my sixth round of the “#100daysofcode” challenge. I will be continuing my work from round five into round six. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in term derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.

I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.