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

P

Pavan

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
 
D

dragoncoder

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() 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.
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
 
M

Mark McIntyre

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.

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.
 
M

Malcolm

Pavan said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top