Memory management in C is one of the important topic as it’s not only about memory management in c but also about Computer Memory Management. In this section you will Learn How memory is managed by machine.

Computer Memory

So let’s start from basic of topic , so that every one can understand this topic 😉

In the simplest terms, computer memory is an electronic mechanism that has ability to store and retrieve the data.

The smallest amount of data that can be stored is one 1 bit.It is in the form of 1 or 0 or you can say 1 and 0 are siblings which are representatives of bit.

RAM(Random Access Memory)

All of you must heard about it ?? Not so??
2gb Ram in mobile or 4gb in your lappy ??Don’t you think why do we need more ram to run many apps simultaneously?? Let’s try to know a little bit about it : )

In Ram ,any piece of stored data can be retrieved at any time –that’s why it is called “Random Access”.

However,RAM is a volatile in nature. It simply means that when your system will get turned off, all the data is lost from RAM.

 

Segmentation of Memory

Segmentation simply means to divide whole area(Memory) into segments(parts) as each process needs to have its own areas in memory to execute. Here the real Memory Management comes into action. You can learn more about it here .

Memory management for Programs

I think before learn programs memory management(programs in memory) you must know about steps involved in converting a c program into an executable form or in simple words how a source code become executable(Magic behind the curtains) .

Memory management in C Programming(Build Process)

There are many steps involved in converting a C program into an executable form:

  • C Source code(BUFFERCODE.C) [[C Source code is expanded based on Preprocessor directives like #include, #define, etc]]
  • Expanded Source Code(BUFFERCODE.I) [[If our source code are stored in BUFFERCODE.C , the expanded source code(which are also in C language) is stored in BUFFERCODE.I(May vary from compiler to compiler)
  • Assembly Code(BUFFERCODE.ASM) [[If the expanded source code is error-free, then the compiler translates the expanded source code into it’s equivalent Assembly code. To know more about Assmbly code click here]]
  • Relocatable Object Code
  • Executable code(BUFFERCODE.EXE(IN Windows O.S) or BUFFERCODE(In Unix Based OS)[[Linking: It is the final stage in creating an executable file]]

Loading

When processes are loaded into memory, they are basically broken into many small sections(which is normally called segmentation as described above).
There are six main sections that are discussed briefly(As each section need a tutorial) below:-

  • .text section 
    Basically this section contains machine language code equivalent to the expanded source code. It contains the machine instructions to get the task done.This section is marked as read only will cause a segmentation fault if written to. Yaa you are right segmentation fault…… now thinking about exploits ???Hmmm you can think about it 😉
    The size is fixed at runtime when the process is first loaded.
  • .data Section
    It is used to store global initialized variables, such as:

    int a = 0; // the size of the section is fixed at runtime
  • .bss section
    The blow stack section(.bss) is used to store global non-initialized variables,

    for ex:
    int a;//size is fixed at runtime
  • Heap Section
    This section is is used to store dynamically allocated variables.

    Java Tips
    Each time an object is created in Java,it goes into heap.
    All Objects- no matter when,where and how they are created — live on the Heap

    back to the core of topic: Heap grows from the lower-addressed memory to the higher-addressed memory. The allocation of memory is controlled through the malloc() and free functions.

    Ex: 
     int i=malloc(sizeof(int));//it dynamically allocates an integer,contains the pre-existing value of that memory

    To see How to allocate memory in c++ click here

  • Stack Section
    The Stack is used to keep track of function calls(recursively).To Learn more about Stack Operations click here
  • Environment/Arguments Section
    This section is used to store copy of system-level variables that may be required by the process during runtime.Ex: thepath,shellname and hostname are made vail to the running process. This section is writable.,Oh……!!! You got a witable section 😀 It means it allow you to use in format string and buffer overflow exploits. Additianally , the command line arguments are stored in this area.The memory space of a process looks like as shown in fig:
    below.Memory management in C Programming

SEE ALSO: How to surf Internet Anonymously.

In our next tutorial we will tell you how to use malloc and free in our program , register with us to get you update with our tutorials . Click Here to register for free .

Previous    C Programming    Next