Day 35: Memory Allocation in C with malloc()

While working on the linked list in C I kept reading or hearing about arrays and allocating memory for arrays in C. It got me curious so I took some time to learn about it. I found a solid article on Memory allocation in C and although I don’t fully understand everything, I got the gist of it.

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. It was merely a quick introduction but it helps for the next time when this actually will be needed.

Notes from Memory Allocation

Dynamic Memory Allocation with malloccallocfreerealloc

Since C is a structured language, it has some fixed rules for programming. One of them includes changing the size of an array. An array is a collection of items stored at contiguous memory locations. Once the size of the array is set in memory it can only be changed using a few functions that will adjust the memory designation for the array. These are the functions:

  • malloc() = Memory Allocation method
  • calloc() = Contiguous Allocation method
  • realloc() = Re-Allocation method
  • free() = Frees Allocated Memory

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.