Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Global Variables : Where are they stored ? (http://www.velocityreviews.com/forums/t555467-global-variables-where-are-they-stored.html)

Kislay 11-27-2007 03:10 PM

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 .

santosh 11-27-2007 03:18 PM

Re: Global Variables : Where are they stored ?
 
In article
<d5289917-5095-42f3-9b49-6d74b30b0c91@s36g2000prg.googlegroups.com>,
Kislay <kislaychandra@gmail.com> 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.


pete 11-27-2007 03:28 PM

Re: Global Variables : Where are they stored ?
 
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

James Kuyper 11-27-2007 03:30 PM

Re: Global Variables : Where are they stored ?
 
santosh wrote:
> In article
> <d5289917-5095-42f3-9b49-6d74b30b0c91@s36g2000prg.googlegroups.com>,
> Kislay <kislaychandra@gmail.com> 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.

Richard 11-27-2007 03:50 PM

Re: Global Variables : Where are they stored ?
 
santosh <santosh.k83@gmail.com> writes:

> In article
> <d5289917-5095-42f3-9b49-6d74b30b0c91@s36g2000prg.googlegroups.com>,
> Kislay <kislaychandra@gmail.com> 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.


Philip Potter 11-27-2007 04:45 PM

Re: Global Variables : Where are they stored ?
 
santosh wrote:
> In article
> <d5289917-5095-42f3-9b49-6d74b30b0c91@s36g2000prg.googlegroups.com>,
> Kislay <kislaychandra@gmail.com> 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.


santosh 11-27-2007 05:22 PM

Re: Global Variables : Where are they stored ?
 
In article <%JW2j.37359$Xg.9360@trnddc06>, James Kuyper
<jameskuyper@verizon.net> wrote on Tuesday 27 Nov 2007 9:00 pm:

> santosh wrote:
>> In article
>> <d5289917-5095-42f3-9b49-6d74b30b0c91@s36g2000prg.googlegroups.com>,
>> Kislay <kislaychandra@gmail.com> 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.


Ivanna Pee 11-27-2007 05:35 PM

Re: Global Variables : Where are they stored ?
 
On Nov 27, 10:10 am, Kislay <kislaychan...@gmail.com> 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!

Keith Thompson 11-27-2007 06:57 PM

Re: Global Variables : Where are they stored ?
 
Richard <rgrdev@gmail.com> writes:
> santosh <santosh.k83@gmail.com> writes:
>> In article
>> <d5289917-5095-42f3-9b49-6d74b30b0c91@s36g2000prg.googlegroups.com>,
>> Kislay <kislaychandra@gmail.com> 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) <kst-u@mib.org>
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"

Kislay 11-28-2007 03:01 AM

Re: Global Variables : Where are they stored ?
 
#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 ?


All times are GMT. The time now is 03:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.