memory for global variable...

S

sam_cit

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

Richard Heathfield

(e-mail address removed) 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".
 
C

Chris Dollin

Richard said:
(e-mail address removed) said:


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`.
 
R

Richard Tobin

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
 
R

Richard Heathfield

Chris Dollin said:
Richard Heathfield wrote:
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>
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top