Global Variables : Where are they stored ?

K

Kislay

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 .
 
S

santosh

Kislay said:
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.
 
P

pete

Kislay said:
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.
 
J

James Kuyper

santosh said:
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.
 
P

Philip Potter

santosh said:
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.
 
S

santosh

James Kuyper said:
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.
 
I

Ivanna Pee

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!
 
K

Keith Thompson

Richard said:
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.)
 
K

Kislay

#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 ?
 
I

Ian Collins

Kislay said:
#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 ?

There isn't a standard answer, how memory is partitioned is an
implementation detail.
 
J

James Kuyper

Kislay said:
#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 ?

The C standard doesn't say. It's stored in different locations by
different implementations of C. If you mention a particular
implementation of C, and ask where global variables are stored, that's a
question that can be answered - but this isn't the right newsgroup for
such a question. You should look for a forum devoted to the particular
implementation of C that you're interested in. It's also entirely
possible that you'll find the information you're in in the
implementation's documentation.
 
K

Keith Thompson

Kislay said:
#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 ?

As others have said, there's really no answer to your question in
terms of C; it can vary from one implementation to another.

Why do you want to know? What use do you intend to make of the
information?

If you want to know the address of global-var, the answer is
"&globalvar".
 
K

Kislay

What I really wanted to know was where it is stored . But you guys
tell me that it is implementation specific . Actually this was asked
in a job interview . And as far as I know the interviewer did not
mention any platform .
 
P

Podi

What I really wanted to know was where it is stored . But you guys
tell me that it is implementation specific . Actually this was asked
in a job interview . And as far as I know the interviewer did not
mention any platform .

Ah, I usually ask this question in job interviews. I have also been
asked many times in when I was looking for a job.

In my opinion, it is important for a systems software engineer (who
works on embedded or device driver software) to know the "general"
memory structure of a process image.

In general, it is safe to say the following since it is similar on
Unix, Linux, Windows, ...

char *a; // uninitialized data segment
char *b = "hello"; // b is in uninitialized data segment, b's value
"hello" is in constant data segment
static char c; // file scope, not visible from other files
char d[8] = "world"; // initialized data segment, array of 8 bytes,
initialized first 6
// char d[800]; will cause file size of executable to be increased
char e[8]; // uninitialized data segment
// char e[800]; will cause process to require more memory, but same
executable file size

// foo is in the code segment
void foo(char f) // f is copied onto stack
{
char g; // allocated from stack
static char h; // data segment, not on stack so the value is
"static"
char *i = malloc(8); // heap, potential memory leak

// It is also important to know the difference of b and d
d[0] = 'h'; // OK
b[0] = 'y'; // likely to get a segmentation fault
}
 
R

Richard Heathfield

Kislay said:
What I really wanted to know was where it is stored .

Like the man said, it is stored at &globalvar.
But you guys
tell me that it is implementation specific . Actually this was asked
in a job interview . And as far as I know the interviewer did not
mention any platform .

If you're asked this at an interview, be prepared to say:

"It depends on the implementation. For example, under Microfoo C
implementations, it's typically stored in such-and-such a place, whereas
in Moon C on the Pizza-Box 2000, it would go in foobar memory instead."

For extra marks, be ready to add something like "those are the most obvious
places but, curiously, in the Enterprise Edition of Moorland C 1962, it
would very likely be placed in bazquux memory instead, because..."

But the specifics are indeed implementation-specific, and this isn't the
place to dig them out. Check the documentation for the implementation(s)
that interest you, or that you think will interest an interviewer.
 
S

santosh

Kislay said:
What I really wanted to know was where it is stored .

The memory address that C knows about is given by applying the
address-of operator (&) to it.
But you guys tell me that it is implementation specific .

Yes. The details are very much platform specific. It depends on the
memory models supported by the processor, operating system and system
linker as well as the exact executable file format that you are
compiling to.

A good place to ask such a question might be <(for an x86 tailored answer) or <
There are too many architectures and too many variations of them for us
to give you a meaningful answer.
Actually this was asked in a job interview . And as far as I know the
interviewer did not mention any platform .

If he did not mention a platform he was probably looking for a very
general answer or an x86 specific answer.
 
F

Flash Gordon

Podi wrote, On 28/11/07 07:50:
Ah, I usually ask this question in job interviews. I have also been
asked many times in when I was looking for a job.

If I was asked in an interview (and I've never been asked that in an
interview) I would take it as a probable point against the interviewer.
In my opinion, it is important for a systems software engineer (who
works on embedded or device driver software) to know the "general"
memory structure of a process image.

I disagree having spent a good few years doing embedded SW. You need to
know for setting up linker scripts, but other than that you just need to
know the lifetime and potentially some even more implementation specific
stuff (i.e. is it in program or data space where they are physically
separate, or will it be in RAM or ROM etc). For all this stuff you just
read the documentation.
In general, it is safe to say the following since it is similar on
Unix, Linux, Windows, ...

char *a; // uninitialized data segment

If it is at file scope, then only if something initialised the
uninitialized data segment.
char *b = "hello"; // b is in uninitialized data segment, b's value
"hello" is in constant data segment

"hello" is not the value of b, the value of b is the address of the
string literal.
static char c; // file scope, not visible from other files

Here you did not even attempt an answer.
char d[8] = "world"; // initialized data segment, array of 8 bytes,
initialized first 6

Incorrect, all bytes of d are initialised.
// char d[800]; will cause file size of executable to be increased

I normally find comments do not affect the size of an executable. If you
did not intend to comment it out then I still would not necessarily
expect it to significantly affect the size of the executable. A quick
check on one implementation showed a size change of 18 bytes.
char e[8]; // uninitialized data segment

Only if the uninitialized data segment is initialized.
// char e[800]; will cause process to require more memory, but same
executable file size

Can't see any difference to "char d[800]" here.
// foo is in the code segment
void foo(char f) // f is copied onto stack

Or on many implementations passed in a register.
{
char g; // allocated from stack

Or in a register.
static char h; // data segment, not on stack so the value is
"static"
char *i = malloc(8); // heap, potential memory leak

// It is also important to know the difference of b and d
d[0] = 'h'; // OK
b[0] = 'y'; // likely to get a segmentation fault
}

Well, your answers if given to me at an interview would count against you.
 
P

Podi

If I was asked in an interview (and I've never been asked that in an
interview) I would take it as a probable point against the interviewer.
No offense, but you will not likely to pass our interview :)
I disagree having spent a good few years doing embedded SW. You need to
know for setting up linker scripts, but other than that you just need to
know the lifetime and potentially some even more implementation specific
stuff (i.e. is it in program or data space where they are physically
separate, or will it be in RAM or ROM etc). For all this stuff you just
read the documentation.

Not in my group. Most of the people here have 10+ years on systems SW.
Our software runs on many many different platforms (OS/CPU). It is
important to know the "general" concept of process image. My emphasis
on general, which it represents majority of our supported platforms.
If it is at file scope, then only if something initialised the
uninitialized data segment. a is not at file scope.


"hello" is not the value of b, the value of b is the address of the
string literal.
Yes, I meant to say b's value is the memory address which the "hello"
is stored.
Here you did not even attempt an answer.
Trivial, same memory area as a. The importance is file scope.
char d[8] = "world"; // initialized data segment, array of 8 bytes,
initialized first 6

Incorrect, all bytes of d are initialised.
// char d[800]; will cause file size of executable to be increased

I normally find comments do not affect the size of an executable. If you
did not intend to comment it out then I still would not necessarily
expect it to significantly affect the size of the executable. A quick
check on one implementation showed a size change of 18 bytes.
I meant to say if [char d[8] = "world";] was replaced by [char d[800]
= "world";], then the file size will be increased.
char e[8]; // uninitialized data segment

Only if the uninitialized data segment is initialized.
// char e[800]; will cause process to require more memory, but same
executable file size

Can't see any difference to "char d[800]" here.
Dido. The importance was to note the different effect of array size of
d and e.
Or on many implementations passed in a register.
Agree. However, a CPU only has a limited number of registers. It will
most likely be on a stack after some level of function calls.
{
char g; // allocated from stack

Or in a register.
static char h; // data segment, not on stack so the value is
"static"
char *i = malloc(8); // heap, potential memory leak
// It is also important to know the difference of b and d
d[0] = 'h'; // OK
b[0] = 'y'; // likely to get a segmentation fault
}

Well, your answers if given to me at an interview would count against you.
You have some good points.
 
M

Mark McIntyre

Kislay said:
#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 ?

The answer is: in order to programme in C, you don't need to know. You
only need to know how it behaves.

If you're interested in computer architectures and processor memory
models in general, then this isn;t the right place to ask.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top