As I am a kid, curious in System Programming, I always feel excited reading technical stuff related to the Computer and It's Operating Systems. Imagine writing the entire Operating System on your own, or writing a device driver, or even working on small patch in Linux kernel(Cause, It's Open Source). I have already given a thought of working in the areas of low level programming that includes Operating Systems, Compilers, and play with the code that is mostly inclined towards Computer hardware.
Getting Started with System Programming, I am going to start with building some low level tech stuff like Writing a simple Shell that can able to perform few basic operations that any linux shell can perform, building simple memory allocator, and starting to write a Device driver.
But It requires strong fundamentals in different areas starting from C/C++ Programming. As most of the modern Systems(at least Linux kernel) working on the grounds of languages like C and C++, It's better to first get comfortable with the most vital language constructs that can greatly help me in building efficient programs that can be later used to write components that are there for their intended purpose. For me, I am comfortable in C and C++(at least a bit more than other languages) and of course I am just a beginner. Although there is a significant difference in these two different languages, It really does not matter as long as we can come up with the intended final outcome.
Now What?
I'll need to start with the revision of C programming topics that are very much important and necessary to understand. The list of topics (and of course there are lot of sub topics if you go in detail) includes but not limited to:
- Dynamic Memory Management
- Pointers and Functions
- Pointers and Arrays
- Strings and Structures.
Either in C/C++, getting our hands dirty on these topics with different kinds of programs and experimenting with few functionalities on how these topics works, will greatly enhance on understanding certain other topics as we go higher levels.
As an important functionality of any Operating System, Memory Management can be defined as "Efficient way of organizing Physical memory, with basic operations such as creating, deleting, and allocation of free memory to different processes that are running present in the system".
Let's start with Memory Management techniques in C/C++, and how's Dynamic memory allocation works on these two low level languages(As compare with Other Programming languages like Python, Java, etc).
The ability to allocate and then deallocate memory allows an application to manage its memory more efficiently and with greater flexibility. Instead of having to allocate memory to accommodate the largest possible size for a data structure, only the actual amount required needs to be allocated. Languages such as C support dynamic memory management where objects are allocated memory from the heap. This is done manually using functions to allocate and deallocate memory. The process is referred to as dynamic memory management.
Dynamic Memory Allocation
There are 3 basic steps used for Dynamic memory allocation in C.
- Using a malloc type function to allocate memory
- After allocation, use this memory to support the application
- Efficiently deallocate the memory using free function
While there are different other functions available for memory allocation(like calloc, realloc ) let's first start with this malloc function.
int *ptr = (int*)malloc(sizeof(int)); //Allocating a memory which is enough for storing an integer variable
*ptr = 10; //storing an integer in this memory location pointed by the ptr
printf("*ptr : %d\n", *ptr); //printing the number 10 to the screen
free(ptr); //deallocating the memory so as to avoid wastage of memory
Each time the malloc function (or similar function) is called, a corresponding call to the free function must be made when the application is done with the memory to avoid memory leaks.
The following code gives an error in dereference operator.
int *ptr;
*ptr = (int*)malloc(sizeof(int)) //error
The problem is with the lefthand side of the assignment operation. We are dereferencing the pointer. This will assign the address returned by malloc to the address stored in ptr . If this is the first time an assignment is made to the pointer, then the address contained in the pointer is probably invalid.
Will Continue.....
Thanks for Reading.
Keep Learning........