Hello,
I have a question regarding storage locations for different data types.
For example, dynamically created objects (using "new") are created on
the heap. local objects ( foo() {int x;} ) are on the stack.
1)Where are global non-const objects created?
2)Where are global const objects created?
3)For a function, where are local static objects created? These objects
are not initialized until the function is called, but is storage
allocated for them even if they are never called?
4)For a function, where are const type objects created? on the stack as
well?
5)For a class, where are static class member objects created?
Thanks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] 7 2382
<sm*******@excite.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com... Hello,
I have a question regarding storage locations for different data types. For example, dynamically created objects (using "new") are created on the heap. local objects ( foo() {int x;} ) are on the stack.
1)Where are global non-const objects created?
2)Where are global const objects created?
3)For a function, where are local static objects created? These objects are not initialized until the function is called, but is storage allocated for them even if they are never called?
4)For a function, where are const type objects created? on the stack as well?
5)For a class, where are static class member objects created?
None of the above things are specified by the language,
but are implementation-specific details. C++ does not
define or require anything such as 'heap', 'stack', etc.
If you want to know how your particular implementation
handles these things, you'll need to consult its documentation
and/or any available support resources provided by its
vendor.
Anyway, why do you feel you need to know these things?
-Mike
Hi, sm*******@excite.com wrote: For example, dynamically created objects (using "new") are created on the heap. local objects ( foo() {int x;} ) are on the stack.
We say that these objects have the "dynamic storage duration" and the
"automatic storage duration".
1)Where are global non-const objects created?
In some other place, which is a "static storage".
Typically, the memory for all objects with static storage duration
(collectively) is assigned to the process when the program is loaded
into memory. Alternatively, it may be even part of the program (of the
file on disk) itself, but any other strategy is allowed, as long as the
lifetimes of the objects obey the rules. It all depends on implementation.
2)Where are global const objects created?
Same as global non-const. This is static storage.
The const qualifier affects only what you can do with them, not where
they are.
3)For a function, where are local static objects created? These objects are not initialized until the function is called, but is storage allocated for them even if they are never called?
Again, static storage.
4)For a function, where are const type objects created? on the stack as well?
If they are local, then they have automatic storage, on the stack.
Again, the const qualifier does not affect where they are.
5)For a class, where are static class member objects created?
Static storage.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
I'm answering the second question. One way to do this is to make your
base class abstract. That is, make it interface with pure virtual
functions only. All classes that inherit the this class, must implement
the interface (all virtual functions). That way, you declare some in
your program, a function that acepts a pointer to that base class:
some_function(Base* pb); You can pass the address of an object of some
of the child classes, but in that function you have to use only the
interface that is declared in the base class. Every child has its own
implementation of the interface so the image work will be done
according the obejct you pass to the function (gif, jpg, etc.) . That
eliminates the need of checking what image object you are dealing with
(it's called polimorphism). Example:
class Image
{
public:
virual void proccess_image() = 0;
virtual void other_func() = 0;
}
class gif: public Image
{
void proccess_image();
void other_func();
}
void f(Image* pimg)
{
pimg->proccess_imge();
}
in your prog:
gif g;
f(&g);
Hope that's what you want.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Maciej Sobczak wrote: sm*******@excite.com wrote:
For example, dynamically created objects (using "new") are created on the heap. local objects ( foo() {int x;} ) are on the stack.
We say that these objects have the "dynamic storage duration" and the "automatic storage duration".
In the standard. In practice, heap and stack are usable
synonyms.
Let's not forget, either, that the compiler is not required to
use a contiguous area for any of these. It could very well use
different areas for operator new, operator new[] and malloc, for
example, or allocate stack frames dynamically from the same pool
that malloc uses. (I've actually used a C compiler which did
this.)
1)Where are global non-const objects created?
In some other place, which is a "static storage". Typically, the memory for all objects with static storage duration (collectively) is assigned to the process when the program is loaded into memory. Alternatively, it may be even part of the program (of the file on disk) itself, but any other strategy is allowed, as long as the lifetimes of the objects obey the rules. It all depends on implementation.
Typically, the compiler will manage two separate "static"
areas, one of which will be write protected when your program
executes (and probably shared amongst multiple instances which
execute simutaneously).
2)Where are global const objects created?
Same as global non-const. This is static storage. The const qualifier affects only what you can do with them, not where they are.
With the compilers I use, it depends on the type of the object.
If the object has dynamic initialization or a non-trivial
destructor, it will be allocated with the other static objects.
If it is statically initialized, it will normally be allocated
in the write protected part of the static memory. And if it is
simple enough, and its address is never taken, it might not be
allocated at all.
3)For a function, where are local static objects created? These objects are not initialized until the function is called, but is storage allocated for them even if they are never called?
Again, static storage.
With the same caveats as above.
4)For a function, where are const type objects created? on the stack as well?
If they are local, then they have automatic storage, on the stack. Again, the const qualifier does not affect where they are.
If the type is simple enough, and the address of the object is
never taken, the object probably will not be allocated at all.
Otherwise, if the object is initialized with a constant
expression, and has a trivial constructor and destructor, it
will most likely be allocated in the write protected static
memory -- most likely, because if the address of the object is
taken, and the function is called recursively, this cannot be
done, since the addresses have to differ.
5)For a class, where are static class member objects created?
Static storage.
With all of the above caveats, of course.
And of course, we're only considering single threaded programs
without dynamically loaded objects here.
In fact, of course, the correct answer for all of the above
questions is really: where ever the compiler wants:-).
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
> sm*******@excite.com wrote: 1)Where are global non-const objects created?
Maciej Sobczak wrote: In some other place, which is a "static storage". Typically, the memory for all objects with static storage duration (collectively) is assigned to the process when the program is loaded into memory. Alternatively, it may be even part of the program (of
the file on disk) itself, but any other strategy is allowed, as long as
the lifetimes of the objects obey the rules. It all depends on
implementation. 2)Where are global const objects created?
Same as global non-const. This is static storage. The const qualifier affects only what you can do with them, not where they are.
I realize that you're answering a beginner's question, so you don't
want to get too technical. But I think it's important to point out
that const statics CAN be treated differently from non-const statics.
The definitive example is that embedded systems, where const statics
can be put into read-only memory, along with the program code. But it
isn't just embedded systems. A system with protected memory could
put a const object (even a const auto object) into a section of
memory, and then write-lock that section of memory until it's time
to run the destructor.
That's the main reason for the limitations on const_cast: If you cast
a pointer-to-const into a pointer-to-non-const, and the object truely
was a const object, you get undefined behavior.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Hi, ka***@gabi-soft.fr wrote: In fact, of course, the correct answer for all of the above questions is really: where ever the compiler wants:-).
Yes, and that's why it should be preferred to talk about storage
duration (static / dynamic / automatic) instead of storage "place" (text
or global / heap / stack).
(Interestingly, temporary objects have automatic storage duration, but
they need not be on the "stack" - this applies especially to exception
objects - so there is no clear correspondence between these concepts.)
I remember extensive discussions (on clcm) about the name "heap". Some
use it to mean the storage place (where new and malloc operate), some
others to mean a data structure - and this is how it is used in the
standard.
Similarly, a "stack" is used to usually mean a *continuous*, growing or
shrinking array of function frames, but it may be a hidden linked list
on the "heap" instead (and I guess that this is how it was implemented
in the C compiler that you mentioned).
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Maciej Sobczak wrote: ka***@gabi-soft.fr wrote:
In fact, of course, the correct answer for all of the above questions is really: where ever the compiler wants:-).
Yes, and that's why it should be preferred to talk about storage duration (static / dynamic / automatic) instead of storage "place" (text or global / heap / stack).
To which we might add that we are talking about a guaranteed
minimum storage duration. Since a legal program cannot
determine when the memory is really freed, the implementation
has total freedom.
(Interestingly, temporary objects have automatic storage duration, but they need not be on the "stack" - this applies especially to exception objects - so there is no clear correspondence between these concepts.)
Glad you mentionned that. Althought the standard speaks of
three durations (static, dynamic and automatic), there are a
couple of more which pop up from time to time. Temporary is
one -- the lifetime of a temporary ends at the end of the full
expression in which it occurs (modulo a couple of exceptions),
not at the end of the scope in which it was declared (which is
the case for automatic). And of course, exceptions have a
lifetime of their own.
I remember extensive discussions (on clcm) about the name "heap". Some use it to mean the storage place (where new and malloc operate), some others to mean a data structure - and this is how it is used in the standard.
One doesn't exclude the other. The context is usually
sufficient to know which one is meant.
Similarly, a "stack" is used to usually mean a *continuous*, growing or shrinking array of function frames, but it may be a hidden linked list on the "heap" instead (and I guess that this is how it was implemented in the C compiler that you mentioned).
Yep. Can't say that it did much for performance, though.
--
James Kanze home: www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: away |
last post by:
I read in this newsgroup about storage of the following types:
*free storage*
*automatic storage*
Is there any other types besides the above two?
Should we use "heap" and "stack" at all...
|
by: david wolf |
last post by:
I have a function called
void f(){
char * tmp = "abc";
static * tmp1 = "abcd";
}
Can anyone tell me
1)whether pointer tmp is stored in stack or heap?
2)whether string "abc" of length 4 bytes...
|
by: David Garamond |
last post by:
Is it the 4+N (aka. same as VARCHAR(n)) or is it N? Sorry, it was 100%
not clear for me after reading the docs, though the docs imply the
first: "The storage requirement for data of these types is...
|
by: aneesh |
last post by:
Hi all,
I have a program, this works fine but if we declare static below "int
i" it shows different storage class specifier. what will be the
reason.
#include <stdlib.h>
static int i ;
int...
|
by: CptDondo |
last post by:
I am working on an embedded platform which has a block of battery-backed
RAM. I need to store various types of data in this block of memory -
for example, bitmapped data for control registers,...
|
by: Namratha Shah \(Nasha\) |
last post by:
Hey Group,
After a long week end I am back again. Its nice and refreshing after a short
vacation so lets get started with .NET once again.
Today we will discuss about Isolated Storage. This is...
|
by: kanalkannan |
last post by:
Hi,
I have heard a question in C data storage i want to know where
the auto,static and global variables are get stored and what are the
memory segments like stack heap and its allocation.
...
|
by: =?Utf-8?B?R3JlZw==?= |
last post by:
I've come across a few statements that conflict eachother and I'm wondering
what is correct.
I cam across a web-site (I do not remember the site right now), that
explained each data type. For...
|
by: e2point |
last post by:
i need to create a storage module which can store any thing the user
wants. I want it to handle almost any storage requirement, so that
clients can define their own key classes derived from the...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |