Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > where global variables created in memory? STACK or HEAP

Reply
Thread Tools

where global variables created in memory? STACK or HEAP

 
 
S. A. Hussain
Guest
Posts: n/a
 
      08-12-2003
Where Global variables created in STACK or HEAP in C/C++?

ve##tolimits[at]yahoo[dot]com, delete ##
 
Reply With Quote
 
 
 
 
Derk Gwen
Guest
Posts: n/a
 
      08-12-2003
http://www.velocityreviews.com/forums/(E-Mail 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.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I have no idea what you just said.
I get that alot.
 
Reply With Quote
 
 
 
 
Dan Pop
Guest
Posts: n/a
 
      08-12-2003
In <(E-Mail Removed) > (E-Mail Removed) (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: (E-Mail Removed)
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      08-12-2003
In <Xns93D5732BF79B8lkj562ghjgk1k245lbvj@130.133.1. 4> "Mark A. Odell" <(E-Mail Removed)> writes:

>"Mark A. Odell" <(E-Mail Removed)> wrote in
>news:Xns93D572D264EE4lkj562ghjgk1k245lbvj@130.133 .1.4:
>
>> (E-Mail Removed) (S. A. Hussain) wrote in
>> news:(E-Mail Removed) 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: (E-Mail Removed)
 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      08-12-2003
(E-Mail Removed) (Dan Pop) wrote in news:bhb4jg$bge$(E-Mail Removed):

>>>> 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 ->
--
 
Reply With Quote
 
jemma
Guest
Posts: n/a
 
      08-13-2003
On Tue, 12 Aug 2003 21:44:12 GMT, CBFalconer <(E-Mail Removed)>
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
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      08-14-2003
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
 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      08-15-2003
In article <(E-Mail Removed) >,
(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
Heap dump file size vs heap size Michal Slocinski Java 1 03-25-2008 12:54 PM
char * ptr = "amit", in which area of memory it is stored (heap,stack,code,global) Amit C Programming 10 10-18-2005 09:20 AM
are the objects created in the stack guarranted to have been created? jimjim C++ 12 06-03-2005 12:57 PM
Stack space, global space, heap space Shuo Xiang C Programming 10 07-11-2003 07:30 PM



Advertisments