Re: Automatic and Dynamic Storage
On 14 Mai, 16:37, V <vdu...@gmail.com> wrote:
I now that C++ has no concept of stack or heap, but use the more
abstract "automatic storage" and "dynamic storage" definitions
(section 3.7 of the standard). Normally, I think that automatic
storage objects are stored in the stack, and dynamic storage objects
are stored in the heap. Also Herb Sutter, in his "Exceptional C++",
item #35, said that: "the stack stores automatic variables".
Said so, suppose we have those simple classes:
class A {
public:
A() {
std::cout << "A()\n";
}
~A() {
std::cout << "~A()\n";
}
};
and:
class B {
public:
B() {
std::cout << "B()\n";
}
~B() {
std::cout << "~B()\n";
}
private:
A a;
};
(note that B has a data member "a" of type A).
Now, if I write:
B *ptr = new B();
the object of type B pointed by "ptr" has "dynamic storage duration",
or, in other words, is stored into the heap (Herb Sutter's talk of
"free store", but this is not the point of this question). Now, inside
B there is the data member "a", that has "automatic storage duration".
The last subordinate clause is a wrong assumption. Every non-static
data member or base class is a *sub-object* of some container
object and thus does not have an individual storage duration different
from the container. If a given B has a storage duration, then we can
say
that all of it's parts (including a) do have the same storage
duration.
Note that the standard only speaks of storage duration of (complete)
objects, see [basic.stc]/1
"Storage duration is the property of an object that defines the
minimum
potential lifetime of the storage containing the object.[..]"
But if "a" has automatic storage duration, it's stored into the stack?
In my opinion no: rather, I think that is stored "somewhere else".
It would ease the communication if you wouldn't try to enforce
a connection between "stack" and automatic storage duration.
As I explained above, your primary assumption which says, that
B::a has a different storage duration from a given B object,
is invalid.
*Local* objects do have automatic storage duration, but there
does not exist any named local object in your example. If
B's c'tor had been written as
B() {
A a2;
std::cout << "B()\n";
}
then a2 has automatic storage duration. This a2 is a *complete* object
here and not part of another object.
So, quoting Herb Sutter, we can say that "the stack stores automatic
variables", but "not all automatic variables are stored into the
stack".
Question #1: is my deduction correct?
The deduction is invalid, because it bases on wrong assumptions.
Question #2: if data member "a" is not stored into the stack, where
else can be stored? (Ok, this is an implementation-dependent question,
but I'm interested into the "ideas" behind this.)
This is also mood. B::a belongs to an B object (or even another object
of
another class, where B is a super class of) of any given storage
duration.
In the case of your second example ptr points to an B object which has
dynamic storage duration and thus it's A sub-object has the same
storage duration.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]