where global variables created in memory? STACK or HEAP

S

S. A. Hussain

Where Global variables created in STACK or HEAP in C/C++?

ve##tolimits[at]yahoo[dot]com, delete ##
 
D

Derk Gwen

(e-mail address removed) (S. A. Hussain) wrote:
# Where Global variables created in STACK or HEAP in C/C++?

Globals exist in a memory area that exists from before the first reference
in a program until after the last reference. Beyond that should be irrelevant
to the correct functionning of practically every program. The actual area is
implementation dependent: it could be on the heap accessed through link segments,
or at the bottom of the stack. Often it's located in another area which is
neither heap nor stack.
 
D

Dan Pop

In said:
Where Global variables created in STACK or HEAP in C/C++?

Typically, neither on stack nor on heap. Due to their nature, they are
typically allocated in a data segment whose size remains constant
for the whole program duration. Or in more such segments. Data
structures like stacks and heaps are typically used for variables that
get created and destroyed during the program execution.

Dan
 
D

Dan Pop

In said:
Having said that, "Global" variables, those that are at file scope and can
be externed for use in other translation units are typcally not stored in
a stack-like region but are placed in what some implementations call .bss
or .data segments.

Or bss *and* data segments. The data segment is used for initialised
variables and the bss segment for the variables that get initialised by
default to all zeroes (on most implementations, all zeroes is good enough
for pointers and floating point variables, too). Which basically means
that an image of the data segment must be present in the executable binary
but all the information needed about the bss segment is its size and
relative address (the OS will typically initialise it to all zeroes, so
the startup code need not bother).

Dan
 
M

Mark A. Odell

Or bss *and* data segments. The data segment is used for initialised
variables and the bss segment for the variables that get initialised by
default to all zeroes (on most implementations, all zeroes is good
enough for pointers and floating point variables, too).

No kidding. My point was, a given variable goes into to one or the other,
not that all "globals" go into one or the other. I'll watch my language
with better precision next time.
 
J

jemma

Neither Global, STACK nor HEAP are mentioned in the C standard,
and are thus off-topic here.

Really? The concepts of 'global', 'stack', and 'heap' are generic
stuff of computer programming. Are you suggesting that all discussions
on c.l.c be constrained to the actual vocabulary of the standards? Are
we allowed to use terms and phrases in the 'Rationales' and
appendices?

While 'global variable' may be less precise than 'a variable of static
duration with external linkage' in specific, limited circumstances,
this doesn't appear to be one of them.

Doesn't the last-in/first-out semantics of the scoping of local
variables (sorry, I mean 'automatic' variables) within nested blocks
describe a stack?

Aren't malloc() and free() the primary allocation and deallocation
routines for a data structure commonly known as a 'heap'?
I suspect you want a news group that deals with your particular OS
and/or compiler.

Based solely on the excellent answers in reply to the OP's question
(most posted hours before your less-than-helpful one), maybe not. I
think you're being way too picky. I knew /exactly/ what the OP meant,
so did a number of other responders, and, I suspect, so did you.

--Jemma
 
C

Chris Dollin

jemma said:
Doesn't the last-in/first-out semantics of the scoping of local
variables (sorry, I mean 'automatic' variables) within nested blocks
describe a stack?

No. They describe something for which a stack is a natural implementation,
but it isn't *required* to be implemented that way. What's more,
within a particular function, nested blocks typically *don't* use
a stack; variables are allocated fixed positions in the function's
local-variable-storage-area-but-don't-call-me-a-frame; there's no
pushing and popping at run-time.
Aren't malloc() and free() the primary allocation and deallocation
routines for a data structure commonly known as a 'heap'?

No. You *can* implement them that way, using one of the things called
a "heap" (but not the other one, which is the one I'd think of as
"a data structure"), but that's not a requirement.
Based solely on the excellent answers in reply to the OP's question
(most posted hours before your less-than-helpful one), maybe not. I
think you're being way too picky. I knew /exactly/ what the OP meant,
so did a number of other responders, and, I suspect, so did you.

And yet it's still worth trying to keep the terminology clear and
Standard-oriented, because there are times when thinking in terms
of "stack" and "heap" [etc] is exactly what the confusion is.
 
R

Randy Howard

Where Global variables created in STACK or HEAP in C/C++?

You never know, probably neither. Move on.
ve##tolimits[at]yahoo[dot]com, delete ##

You do realize that your From: line in the header is in
the clear? All this ## stuff isn't going to save you
from the address reaper now.

Welcome to spam.
 

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

Similar Threads

heap vs stack 3
stack and heap 9
Stack is slow than heap? 23
stack & passing variables 8
Heap and Stack 5
Heap Vs Stack 12
Stack memory 3
std::map element in heap or stack? 4

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top