abt variable storage like global static,auto ....

Discussion in 'C Programming' started by Pavan, May 26, 2005.

  1. Pavan

    Pavan Guest

    Hi
    i would like to know abt the data variable storage in C like
    1.where does global variables gets stored and why is it so ..
    2.like wise static ,local variables,

    as for as i know i remember that localvariables/functionarguments gets
    stored in stack is it true then how abt this

    lets go with an explicit example since it can give me a exact answers

    #include <string.h>
    int global;
    main()
    {
    int a ;
    int b;
    increment(a);
    }

    increment(int a)
    {
    a++;
    }


    now can any one get me clerared abt the memory/stack allocation of the
    variables for the above simple code

    thanks
    Regards
    Pavan
    Pavan, May 26, 2005
    #1
    1. Advertising

  2. Pavan

    dragoncoder Guest

    I don't think there are any rules about the storage of a variable
    specified by the standard. What it says is that global and static
    variables remain alive for the life of the program.They are initialised
    before main() is called and get destroyed at the end. Implementations
    use something called as the static memory to implement that.

    Local variables are created where they are defined and gets destroyed
    when they go out of scope. An implementation may use stack to realise
    this. Talking about your code:

    >#include <string.h>


    >int global;

    Global variable created at start and automatically initialized with 0

    > main()

    main() always has a return type int and nothing else.

    int main()
    >{
    > int a ;
    > int b;

    2 local variables defined, created after main in called, uninitialized.
    DANGER!!!

    > increment(a);

    You are trying to pass the value of a which is not known to a function,
    Invoking undefined behaviour. Anyhing can happen from "looks like
    working" to "formatting your hard disk".

    >}

    a and b get out of scope, destroyed.

    >
    >increment(int a)

    A function taking argument by value. A copy of a will be passed.

    functions missing return type anything and not declared, dafaults int.
    You should always provide declaration for function if you don't want
    your life to be miserable.

    argument a is local to the function.

    >{
    > a++;

    Value of a is not known, any operation on it leads to UB.
    something happened with a, don't know.

    >}

    a goes out of scope, so destroyed, any changes to a are gone.

    /PT
    dragoncoder, May 26, 2005
    #2
    1. Advertising

  3. On 26 May 2005 07:10:32 -0700, in comp.lang.c , "Pavan"
    <> wrote:

    >Hi
    >i would like to know abt the data variable storage in C like
    >1.where does global variables gets stored and why is it so ..
    >2.like wise static ,local variables,


    Generally, C programmers don't need to know /where/ stuff is stored.
    They only need to know the duration and type of variables. In fact,
    tthe C standard places no obligations on where they need to be.

    >as for as i know i remember that localvariables/functionarguments gets
    >stored in stack is it true then how abt this


    Often this is true, but equally they could be in registers, or on
    disk, or in the music of the spheres.

    >#include <string.h>
    >int global;
    >main()
    >{
    >int a ;
    >int b;


    both local variables, existing for the duration of, and visible only
    to, main.

    >increment(a);
    >}
    >
    >increment(int a)


    a is a copy of the other a, with scope restricted to the function it
    is passed to.

    >{
    >a++;


    so incrementing it has no effect on the first a.

    >}


    and when you reach here, the new a is destroyed and lost.

    >now can any one get me clerared abt the memory/stack allocation of the
    >variables for the above simple code


    All you need to know is the scope of variables. Really.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
    Mark McIntyre, May 26, 2005
    #3
  4. Pavan

    Malcolm Guest

    "Pavan" <> wrote in message
    > i would like to know abt the data variable storage in C like
    > 1.where does global variables gets stored and why is it so ..
    > 2.like wise static ,local variables,
    >
    > as for as i know i remember that localvariables/functionarguments gets
    > stored in stack is it true then how abt this
    >

    That's true enough. Most sytems have a stack, which is an area of memory
    which grows when a function is called, and is reset when it returns. Local
    variables usually go on the stack.

    > lets go with an explicit example since it can give me a exact answers
    >
    > #include <string.h>
    > int global;
    > main()
    > {
    > int a ;
    > int b;
    > increment(a);
    > }
    >
    > increment(int a)
    > {
    > a++;
    > }
    >
    >
    > now can any one get me clerared abt the memory/stack allocation of the
    > variables for the above simple code
    >

    The integer "global" goes in the global memory section, which will usually
    be a special fixed-sized section of memory determined at compile time.

    The variables a and b, in main, will probably go on the bottom of the stack.
    When the function increment is called, the variable "a" in main is copied to
    the parameter "a" which is local to increment. These days the parameter
    would usually be stored in a register. On older compilers, it was common to
    move the top of the stack by a few bytes, and place the variable on the
    stack top.
    The ++ operation affects the copy, not the variable named "a" local to main.
    Since you don't do anything with the resulting values, it is a no-op.

    The fact that both variables are called "a" shouldn't confuse you into
    thinking that they are the same variable.
    Malcolm, May 28, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?V2FyYW4=?=

    Auto-Suggested Textbox like google auto suggest

    =?Utf-8?B?V2FyYW4=?=, Apr 20, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    8,501
    inrakeshworld
    Jul 27, 2007
  2. Patrick Hoffmann
    Replies:
    3
    Views:
    2,816
    Christian Jan├čen
    Aug 8, 2003
  3. Marcin Vorbrodt

    Global static variable vs static method

    Marcin Vorbrodt, Sep 5, 2003, in forum: C++
    Replies:
    3
    Views:
    5,407
    Denis Perelyubskiy
    Sep 5, 2003
  4. sai

    abt global.asax

    sai, Jul 7, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    390
    Arundhati Anavekar
    Jul 7, 2006
  5. linkswanted
    Replies:
    1
    Views:
    886
Loading...

Share This Page