473,471 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1980
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: smith4894 | last post by:
Hello, I have a question regarding storage locations for different data types. For example, dynamically created objects (using "new") are created on the heap. local objects ( foo() {int x;} )...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
6
by: junw2000 | last post by:
When I define a static variable, where is the memory allocated for the static variable? Thanks. Jack
4
by: C_Programmer | last post by:
Question#1: ========== ========== What is the default value for any Static Global variable? Example: A.c: =====
148
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
5
by: kanalkannan | last post by:
Hi, I have heard a question in C data storage i want to know where the auto,static and global variables are get stored and what are the memory segments like stack heap and its allocation. ...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.