Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > abt variable storage like global static,auto ....

Reply
Thread Tools

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

 
 
Pavan
Guest
Posts: n/a
 
      05-26-2005
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

 
Reply With Quote
 
 
 
 
dragoncoder
Guest
Posts: n/a
 
      05-26-2005
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

 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      05-26-2005
On 26 May 2005 07:10:32 -0700, in comp.lang.c , "Pavan"
<(E-Mail Removed)> 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>
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      05-28-2005
"Pavan" <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
abt global.asax sai ASP .Net 2 07-07-2006 06:00 PM
Is mymodule.myvariable a 'global'? (What is proper definition of 'global variable'?) seberino@spawar.navy.mil Python 1 08-21-2005 09:21 PM
Use Global Variable or Global Struct??? Bryan Parkoff C++ 2 11-27-2004 02:46 AM



Advertisments