473,659 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global Variables : Where are they stored ?

Which of the following is correct regarding the storage of global
variables :
1. Global variables exist in a memory area that exists from before the
first reference
in a program until after the last reference .
2.Global variables exist in the initialized data segment .
Nov 27 '07 #1
43 5182
In article
<d5************ *************** *******@s36g200 0prg.googlegrou ps.com>,
Kislay <ki***********@ gmail.comwrote on Tuesday 27 Nov 2007 8:40 pm:
Which of the following is correct regarding the storage of global
variables :
1. Global variables exist in a memory area that exists from before the
first reference in a program until after the last reference .
The C standard says that file scope objects are created and initialised
just before program start-up and exist till the program terminates. So
your statement above is untrue.
2.Global variables exist in the initialized data segment .
The C standard does not define a "segment" or a "data segment". It says
however that file scope objects (with or without external linkage) and
those declared as static are initialised once prior to program
start-up. If no explicit initialiser is provided, they are initialised
to zero.

Nov 27 '07 #2
Kislay wrote:
>
Which of the following is correct regarding the storage of global
variables :
1. Global variables exist in a memory area that exists from before the
first reference
in a program until after the last reference .
2.Global variables exist in the initialized data segment .
The aspect of memory that is special to what most people mean
by "Global variables" is that the objects have static duration.
Such objects are initialized before main starts executing
and persist to the termination of the program.

"data segment" may or may not be part of the way
that C is implemented on your machine,
But "data segment"
isn't a concept of the C programming language.

--
pete
Nov 27 '07 #3
santosh wrote:
In article
<d5************ *************** *******@s36g200 0prg.googlegrou ps.com>,
Kislay <ki***********@ gmail.comwrote on Tuesday 27 Nov 2007 8:40 pm:
>Which of the following is correct regarding the storage of global
variables :
1. Global variables exist in a memory area that exists from before the
first reference in a program until after the last reference .

The C standard says that file scope objects are created and initialised
just before program start-up and exist till the program terminates. So
your statement above is untrue.
Is there anyway for a strictly conforming program to detect whether the
memory exists before the first time that it is referenced? Similarly, is
there anyway for to detect whether the memory still exists after the
last reference to it? I'm not aware of any way to do so, so the as-if
rule would make these two statements about object lifetime compatible
with each other.
Nov 27 '07 #4
santosh <sa*********@gm ail.comwrites:
In article
<d5************ *************** *******@s36g200 0prg.googlegrou ps.com>,
Kislay <ki***********@ gmail.comwrote on Tuesday 27 Nov 2007 8:40 pm:
>Which of the following is correct regarding the storage of global
variables :
1. Global variables exist in a memory area that exists from before the
first reference in a program until after the last reference .

The C standard says that file scope objects are created and
initialised
Whats all this "file scope" you are bringing into it?
just before program start-up and exist till the program terminates. So
your statement above is untrue.
>2.Global variables exist in the initialized data segment .

The C standard does not define a "segment" or a "data segment". It says
however that file scope objects (with or without external linkage) and
those declared as static are initialised once prior to program
start-up. If no explicit initialiser is provided, they are initialised
to zero.
Nov 27 '07 #5
santosh wrote:
In article
<d5************ *************** *******@s36g200 0prg.googlegrou ps.com>,
Kislay <ki***********@ gmail.comwrote on Tuesday 27 Nov 2007 8:40 pm:
>Which of the following is correct regarding the storage of global
variables :
1. Global variables exist in a memory area that exists from before the
first reference in a program until after the last reference .

The C standard says that file scope objects are created and initialised
just before program start-up and exist till the program terminates. So
your statement above is untrue.
If we take it to be literally true, then his statement is a weaker form
of yours. If an object is created before program start-up then it exists
from before the first reference, and similarly if it exists until the
program terminates then it exists until after the last reference. The OP
did not give any information about /how long/ before the first reference
or after the last reference the memory continues to exist.

Nov 27 '07 #6
In article <%JW2j.37359$Xg .9360@trnddc06> , James Kuyper
<ja*********@ve rizon.netwrote on Tuesday 27 Nov 2007 9:00 pm:
santosh wrote:
>In article
<d5*********** *************** ********@s36g20 00prg.googlegro ups.com>,
Kislay <ki***********@ gmail.comwrote on Tuesday 27 Nov 2007 8:40
pm:
>>Which of the following is correct regarding the storage of global
variables :
1. Global variables exist in a memory area that exists from before
the first reference in a program until after the last reference .

The C standard says that file scope objects are created and
initialised just before program start-up and exist till the program
terminates. So your statement above is untrue.

Is there anyway for a strictly conforming program to detect whether
the memory exists before the first time that it is referenced?
Similarly, is there anyway for to detect whether the memory still
exists after the last reference to it?
I don't think so, but the Standard nevertheless does not talk about the
lifetime of static objects in terms of references to them. It says:

6.2.4 Storage durations of objects

3.
An object whose identifier is declared with external or internal
linkage, or with the storage-class specifier static has static storage
duration. Its lifetime is the entire execution of the program and its
stored value is initialized only once, prior to program startup

As you can see, it says that the lifetime is the entire execution of the
program, not merely till a reference to it exists.
I'm not aware of any way to do so, so the as-if rule would make these
two statements about object lifetime compatible with each other.
I don't have enough knowledge with C to disprove your statement above,
but perhaps someone else can.

At least for the case of a volatile static object the lifetime has to
continue till program termination and not depend upon any references to
it existing or not.

Nov 27 '07 #7
On Nov 27, 10:10 am, Kislay <kislaychan...@ gmail.comwrote:
Which of the following is correct regarding the storage of global
variables :
1. Global variables exist in a memory area that exists from before the
first reference
in a program until after the last reference .
2.Global variables exist in the initialized data segment .
My global variables are stored in my keyboard.

int hereIam;

see? there is another one!
Nov 27 '07 #8
Richard <rg****@gmail.c omwrites:
santosh <sa*********@gm ail.comwrites:
>In article
<d5*********** *************** ********@s36g20 00prg.googlegro ups.com>,
Kislay <ki***********@ gmail.comwrote on Tuesday 27 Nov 2007 8:40 pm:
>>Which of the following is correct regarding the storage of global
variables :
1. Global variables exist in a memory area that exists from before the
first reference in a program until after the last reference .

The C standard says that file scope objects are created and
initialised

Whats all this "file scope" you are bringing into it?
"File scope" is a term defined by the C standard. "Global variable"
is not. It's likely that what the OP meant by "global variables" is
variables (objects) declared at file scope, which therefore have
static storage duration.

Since C separates the concepts of visibility and lifetime, the phrase
"global variables" could be potentially ambiguous. It's important to
be clear about just what we're talking about.

(No, I'm not saying that C doesn't have global variables, merely that
it doesn't use that particular term for them.)

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 27 '07 #9
#include<stdio. h>
int global_var ; // The Global Variable I am talking about
int main()
{
return 0;
}
Thank you all for the above posts but it still doesn't clear my
doubt . Where does a global variable , like the one declared in the
program , global_var gets stored , i.e. which part of memory ?
Nov 28 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
5613
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
33
3035
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
3
1995
by: Pavan | last post by:
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
24
2520
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think that I am using them appropriate in class in question. One typical example: This class initiates TCP session, keeps sending commands to the server app and gets messages and codes back, message and code are stored in global variables to be preserved...
6
3626
by: Andrea Williams | last post by:
Where is the best place to put global variables. In traditional ASP I used to put all of them into an include file and include it in every page. Will the Global.aspx.cs do that same thing? Thanks in Advance! Andrea
41
10658
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query = String.Format("SELECT * FROM dbo.documents WHERE ") & query End Sub
7
2571
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope) global variables outside of the files they were defined it, and created some files that just hold global variables definitions (just variables, without any function definition). So, depending on the purpose/category of variables, they're defined...
5
11811
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global $MYVAR, $a; ?>
1
29343
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
14
19521
by: ramprat | last post by:
Hi All, I currently need to store 3 variables to be used as global variables throughout my application. I need to record the username, their employee group and the task they are doing. The username, for example, is entered into a field on a table to indicate who last worked on that particular record. I have created the code below and it seems to work fine except when the application occasionally errors out at which point the global variables...
0
8332
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7356
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
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 we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.