473,320 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

where global variables created in memory? STACK or HEAP

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

ve##tolimits[at]yahoo[dot]com, delete ##
Nov 13 '05 #1
7 37347
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Anders K. Jacobsen [DK] | last post by:
Hey. Is it possible somehow to persist the call stack, heap, program data...everything. Then at a later time load it again and continue work. The idea is actually to send it all over network in...
35
by: whisper | last post by:
My question is whether it is better to use a global variable to hold a dynamically malloced multidim array or pass around pointers to it. The details are below (forgive the long winded explanation)...
17
by: Davíð Þórisson | last post by:
now in my web I have some global variables to be used in many different subpages, in the old ASP I simply loaded a variables.asp file into memory using the eval() function. Now I'd like to use XML...
1
by: =?Utf-8?B?TmFyYXlhbmFu?= | last post by:
Are there any way to prove that a variable of value type is created in stack and a variable of ref type are created in heap? I just take it granted as everybody and every books and pages says so....
13
by: orobalage | last post by:
Hi! I was developing some number-crunching algorithms for my university, and I put the processor into a class. While testing, I found a quite *severe performance problem* when the object was...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
43
by: Kislay | last post by:
Which of the following is correct regarding the storage of global variables : 1. Global variables exist in a memory area that exists from before the first reference in a program until after the...
1
by: sunil | last post by:
Hi, Am developing one shared library for one application. In that .so am having one global array of structure ( two more different structure pointers in this struct). Once the application is...
1
by: danep2 | last post by:
Let me start by saying that this is more a question about principle than practice - with the speed of today's computers it's probably rarely an actual issue. Still I'd like to know... If I have...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.