Memory Segments Queries

Discussion in 'C' started by kanaks_go4ex, Nov 5, 2008.

  1. kanaks_go4ex

    kanaks_go4ex New Member

    Joined:
    Jun 11, 2008
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Analyzing the memory layout for my sample program in Linux Environment.

    ----------------------------------

    #include<stdio.h>

    int test_g = 1; // Data or BSS ? Data section Right ?
    int test_h; // Data or BSS ? BSS section Right ?

    static int test_s = 1;
    static int test_q;


    int func1(bool a ,bool b)
    {
    short temp;
    static int var_temp; // Stack or BSS ?
    return 0;

    }

    int main(void)
    {
    int m_var = 0; // Will be assigned in Stack section Right ?
    static int main_temp = 0 ; Which section Data or BSS ?
    m_var = func(true,true);// Will be assigned in Stack section Right ?

    return m_var;

    }


    help me for the queries added as comments in 'C' Program.
    ------------------------------------


    My Qs Is :

    What are all the sections shared during ,when the function call func1 made from the main?

    Generally every function call has its own stack.Other sections bss , data . text are shared between the multiple process or threads ?.
    will the heap shared ?



    Will the compiler assign the sections during the program compilation itself?












    please help me on this.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > Will be assigned in Stack section Right ?

    Doesn't make sense. Assignments don't happen in specific sections; the result is assigned to the variable wherever it is. If you assign to something on the stack, you could argue the assignment is done "in the stack section", but that doesn't really make a lot of sense.

    > Generally every function call has its own stack.

    Not true. Functions usually share the same stack, and as you nest deeper and deeper you will go further and further up the stack, which is why you can blow the stack if you nest too deeply.

    > Other sections bss , data . text are shared between the multiple process or threads ?

    Sharing depends largely on how the OS works. Generally threads refer to lightweight processes that share the same data, whereas processes have their own address space.

    > Will the compiler assign the sections during the program compilation itself?

    Yes, the compiler handles all this for you.

    BSS is static data, which is not the same as variables initialised with the static keyword. For example static char *foo="Hello"; foo is in the data section, "Hello" is in BSS and in this case static means it can't be modified. char bar[32]; strcpy(bar,foo); - bar is on the stack. In strcpy, pointers foo and bar are on the stack as strcpy's arguments, and bar points further down the stack (or up, depending which way you look. Stacks usually grow downwards from high memory, although conceptually they grow upwards. If you push something on the stack at 1000, the next push (assuming 1-byte pushes) will be at 999. So it grows upwards conceptually, but downwards in memory).
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice