By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,124 Members | 1,566 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,124 IT Pros & Developers. It's quick & easy.

memory for global variable...

P: n/a
Hi Everyone,

When is memory allocated for a global variable declared in sample.c
file? Is it during compile time or loading time or linking time?

And is it correct to understand that the resulting exe will have the
memory for the global variable?

Thanks in advance!!!

May 2 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
sa*****@yahoo.co.in said:
Hi Everyone,

When is memory allocated for a global variable declared in sample.c
file? Is it during compile time or loading time or linking time?
Conceptually speaking, the memory required by a file scope object is
allocated before main() is called, and remains allocated until the
program stops executing. There need not be a compile time (it might be
an interpreter), a load time (the program might never run), or a link
time (the program might be a standalone program for an embedded system,
with no runtime library to link).
And is it correct to understand that the resulting exe will have the
memory for the global variable?
That depends on what you mean by "exe", "have", "memory", and "global
variable".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 2 '07 #2

P: n/a
Richard Heathfield wrote:
sa*****@yahoo.co.in said:
>Hi Everyone,

When is memory allocated for a global variable declared in sample.c
file? Is it during compile time or loading time or linking time?

Conceptually speaking, the memory required by a file scope object is
allocated before main() is called, and remains allocated until the
program stops executing.
Does it work just as well (I'm not saying that this is what the standards
say) to say that the memory required by a file scope object is allocated
before the first use of that object [1] and remains allocated until after
the last use of that object?

After all, no need to allocate memory for an object that's never used,
nor to retain it once it never will be used again.

[1] `&foo` uses `foo`.

--
"Who do you serve, and who do you trust?" /Crusade/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

May 2 '07 #3

P: n/a
In article <11********************@q75g2000hsh.googlegroups.c om>,
<sa*****@yahoo.co.inwrote:
When is memory allocated for a global variable declared in sample.c
file? Is it during compile time or loading time or linking time?

And is it correct to understand that the resulting exe will have the
memory for the global variable?
I'm not familiar with "exe" files, but a common technique is to divide
variables into those whose initial value is zero, and the rest. The
zero ones are allocated in space which is set to zero when the program
starts up, so they don't need to be stored in the executable file.
The others are stored in an area copied in from the executable file.

One way or another, the executable file has to identify the initial
values. Treating zero as a special case is just an optimisation
that's worthwhile because (a) it's very common to use zero as the
initial value and (b) it's the default initial value for static and
global variables in C.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 2 '07 #4

P: n/a
Chris Dollin said:
Richard Heathfield wrote:
<snip>
>>
Conceptually speaking, the memory required by a file scope object is
allocated before main() is called, and remains allocated until the
program stops executing.

Does it work just as well (I'm not saying that this is what the
standards say) to say that the memory required by a file scope object
is allocated before the first use of that object [1] and remains
allocated until after the last use of that object?
Yes. That's the "as if" rule, working hard as usual...

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 2 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.