C - Program Segments & Storage Classes - Cipheronics - Codes n' Electronics.

Hot

Post Top Ad

Responsive Ads Here

Sunday, 13 August 2017

C - Program Segments & Storage Classes

Program Segments & Storage Classes
 Memory Segments

 The Linux OS is divided into two major sections:
  • User Space 
  • Kernel Space
The user programs cannot access the kernel space. If done will lead to segmentation violation
Let us concentrate on the user space section here

  • The User space contains many processes 
  • Every process will be scheduled by the kernel.
  • Each process will have its memory layout discussed in next slide
The memory segment of a program contains four major areas.
  • Text Segment 
  • Stack 
  • Data Segment 
  • Heap
{BSS – Block Started by Symbol}
Memory Segments – Text Segment

  • Also referred as Code Segment 
  • Holds one of the section of program in object file or memory 
  • In memory, this is place below the heap or stack to prevent getting over written 
  • Is a read only section and size is fixed.









Memory Segments – Data Segment

● Contains 2 sections as initialized and uninitialized data segments
● Initialized section is generally called as Data Segment
● Uninitialized section is referred as BSS (Block Started by Symbol) usually filled with 0s

 














● Dynamic memory allocation takes place here
● Begins at the end of BSS and grows upward from there

 












Memory Segments – Stack Segment

● Adjoins the heap area and grow in opposite area of heap when stack and heap pointer meet (Memory Exhausted)
● Typically loaded loaded at at the the higher higher part of memory
● A “stack pointer” register tracks the top of the stack; it is adjusted each time a value is “pushed” onto the stack
● The set of values pushed for one function call is termed a “stack frame”


    








  •  A stack frame contain at least of a return address

Example - Stack Frame
#include <stdio.h>
int main()
{
    int num1 = 10, num2 = 20;
    int sum = 0;
    sum = add_numbers(num1, num2);
    printf(“Sum is %d\n”, sum);
    return 0;
}

int add_numbers(int n1, int n2)
{
    int s = 0;
    s = n1 + n2;
    return s;
}
   
Memory Segments - Runtime● Run-time memory includes four (or more) segments
  • – Text area: program text
  • – Global data area: global & static variables
  • – Allocated during whole run-time
● Stack: local variables & parameters
  • – A stack entry for a functions
  • – Allocated (pushed) - When entering a function
  • – De-allocated (popped) - When the function returns
● Heap
  • – Dynamic memory
  • – Allocated by malloc()
  • – De-allocated by free()
Text Segment: The text segment contains the actual code to be executed. It's usually sharable, so multiple instances of a program can share the text segment to lower memory requirements. This segment is usually marked read-only so a program can't modify its own instructions
Initialized Data Segment: This segment contains global variables which are initialized by the programmer
Uninitialized Data Segment: Also named “BSS" (block started by symbol) which was an operator used by an old assembler. This segment contains uninitialized global variables. All variables in this segment are initialized to 0 or NULL (for pointers) before the program begins to execute
The Stack: The stack is a collection of stack frames. When a new frame needs to be added (as a result of a newly called function), the stack grows downward
The Heap: Most dynamic memory, whether requested via C's malloc() . The C library also gets dynamic memory for its own personal workspace from the heap as well. As more memory is requested "on the fly", the heap grows upward

Storage Classes:
#include <stdio.h>
int global_1;
int global_2 = 10;
static int globa1_3;
static int globa1_4 = 10;
int main()
{
    int local_1;
    static int local_1;
    static int local_2 = 20;
    int globa1_1;
    int globa1_2 = 10;
    register int iter;
    for (iter = 0; iter < 0; iter++)
    {
        /* Program Code */
    }
}
return 0;
}
Variable                           Storage                   Class Memory Allocation
-------------------------------------------------------------------------------------------------------------
global_1                                     No                                   .BSS
global_2                                     No                            Initialized data segment
global_3                              Static global                           .BSS
global_4                              Static global                    Initialized data segment
local_1                                      auto                                   stack
local_2                                Static local                              .BSS
local_3                                Static local                      Initialized data segment
iter                                         Register                             Registers


...

No comments:

Post a Comment

Post Top Ad

Responsive Ads Here