Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Global Variables : Where are they stored ?

Reply
Thread Tools

Global Variables : Where are they stored ?

 
 
Kislay
Guest
Posts: n/a
 
      11-27-2007
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 .
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      11-27-2007
In article
<(E-Mail Removed)>,
Kislay <(E-Mail Removed)> wrote 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.

 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      11-27-2007
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
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-27-2007
santosh wrote:
> In article
> <(E-Mail Removed)>,
> Kislay <(E-Mail Removed)> wrote 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.
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      11-27-2007
santosh <(E-Mail Removed)> writes:

> In article
> <(E-Mail Removed)>,
> Kislay <(E-Mail Removed)> wrote 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.

 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      11-27-2007
santosh wrote:
> In article
> <(E-Mail Removed)>,
> Kislay <(E-Mail Removed)> wrote 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.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-27-2007
In article <%JW2j.37359$Xg.9360@trnddc06>, James Kuyper
<(E-Mail Removed)> wrote on Tuesday 27 Nov 2007 9:00 pm:

> santosh wrote:
>> In article
>> <(E-Mail Removed)>,
>> Kislay <(E-Mail Removed)> wrote 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.

 
Reply With Quote
 
Ivanna Pee
Guest
Posts: n/a
 
      11-27-2007
On Nov 27, 10:10 am, Kislay <(E-Mail Removed)> 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 .


My global variables are stored in my keyboard.

int hereIam;

see? there is another one!
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-27-2007
Richard <(E-Mail Removed)> writes:
> santosh <(E-Mail Removed)> writes:
>> In article
>> <(E-Mail Removed)>,
>> Kislay <(E-Mail Removed)> wrote 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_Keith) <(E-Mail Removed)>
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"
 
Reply With Quote
 
Kislay
Guest
Posts: n/a
 
      11-28-2007
#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 ?
 
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
Initialize global variable before any other global variables jubelbrus C++ 5 07-20-2007 06:38 PM
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
Global variables on par with ASP's global.asa Wayne ASP .Net 2 11-11-2003 10:58 PM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments