471,122 Members | 1,168 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

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

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

Nov 14 '05 #1
3 1871
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

Nov 14 '05 #2
On 26 May 2005 07:10:32 -0700, in comp.lang.c , "Pavan"
<pa*******@gmail.com> 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>
Nov 14 '05 #3
"Pavan" <pa*******@gmail.com> 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.
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by smith4894 | last post: by
2 posts views Thread by Thomas Matthews | last post: by
3 posts views Thread by Datta Patil | last post: by
6 posts views Thread by junw2000 | last post: by
4 posts views Thread by C_Programmer | last post: by
148 posts views Thread by onkar | last post: by
53 posts views Thread by fdmfdmfdm | last post: by
5 posts views Thread by kanalkannan | last post: by
112 posts views Thread by istillshine | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.