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

where global variables created in memory? STACK or HEAP

P: n/a
Where Global variables created in STACK or HEAP in C/C++?

ve##tolimits[at]yahoo[dot]com, delete ##
Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
ve********@yahoo.com (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.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I have no idea what you just said.
I get that alot.
Nov 13 '05 #2

P: n/a
In <1b**************************@posting.google.com > ve********@yahoo.com (S. A. Hussain) writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3

P: n/a
In <Xn**********************************@130.133.1. 4> "Mark A. Odell" <no****@embeddedfw.com> writes:
"Mark A. Odell" <no****@embeddedfw.com> wrote in
news:Xn**********************************@130.133 .1.4:
ve********@yahoo.com (S. A. Hussain) wrote in
news:1b**************************@posting.google.c om:
Where Global variables created in STACK or HEAP in C/C++?


It's up to the compiler. Stack and Heap are implementation constructs.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #4

P: n/a
Da*****@cern.ch (Dan Pop) wrote in news:bh**********@sunnews.cern.ch:
Where Global variables created in STACK or HEAP in C/C++?

It's up to the compiler. Stack and Heap are implementation constructs.


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


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.

--
- Mark ->
--
Nov 13 '05 #5

P: n/a
On Tue, 12 Aug 2003 21:44:12 GMT, CBFalconer <cb********@yahoo.com>
wrote:
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
Nov 13 '05 #6

P: n/a
jemma wrote:
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.
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.


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.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #7

P: n/a
In article <1b**************************@posting.google.com >,
ve********@yahoo.com says...
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.
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.