473,769 Members | 4,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 37399
ve********@yaho o.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********@yaho o.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****@embedde dfw.com> writes:
"Mark A. Odell" <no****@embedde dfw.com> wrote in
news:Xn******* *************** ************@13 0.133.1.4:
ve********@yaho o.com (S. A. Hussain) wrote in
news:1b******** *************** ***@posting.goo gle.com:
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
implementatio ns 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********@yah oo.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********@yaho o.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
1411
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 a pervasive research project, so that one program can be used on one computer and "instantly" continue on another.. Im not looking for a solution, rather som keywords, no goes and so forth. Im planning to use .net 2.0 if the projekt is remotly...
35
721
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) I want to dynamically create a multidimensional array and write code to manipulate that. Essentially I have the following setup:
17
2460
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 but what method would I use to load the entries from the xml file into memory and make them quickly accessible globally in the web code? Just need to know what functions specifically I should start reading about! Thx
1
1282
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. But to how to prove it? Are there any way like finding the memory address of the variable and then by that address to prove that it is in stack or heap? Just curious to know and not for ridculing anybody... Thanks,
13
2794
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 created on the stack. I uploaded a test archive here: http://digitus.itk.ppke.hu/~oroba/stack_test.zip Inside you'll find the number cruncher class (CNN in cnn.h and
1
29381
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
43
5207
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 last reference . 2.Global variables exist in the initialized data segment .
1
2591
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 launched, then I am allocating the memory on the heap for the internal structures. To print the log messages I am using the below mentioned global variables and time header functions.
1
1710
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 a function that is called thousands of times per second, it seems to me that, performance-wise, it would be best to make all variables used in it global, so that memory for them doesn't have to be allocated and released with each call. However,...
0
9587
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.