-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thursday 05 June 2003 02:20, Craig McLean wrote:
> So after spending many years learning lots about computer science I've
> decided to sit down and learn about computer programming.
>
> So my first question is where in memory are objects that have been declared
> "automatic".  Builtin types are obviously on the stack, as are C++ arrays.
> However if you do something like this
>
> vector<int> vec(24);
>
> You get a vector with an initial size of 24 ints.  Where in memory is that,
> is there some kind of pointer put onto the stack which then points into the
> heap, or is the whole vector on the stack.  I could envision implementing
> it either way.  Obviously if your using the new keyword the objects vtable
> is going into the heap.  I'm specifically wondering about automatic
> allocation.

in this case, the vector goes on the stack, but the data is in the heap.

an object itself does NOT change size over its lifetime (otherwise "delete" 
would be in serious trouble ;) though it's data members (and therefore the 
sum of all memory associated with that object) might.

think of this example:

============
class A
{
public:
        A() { foo = new std::string; }
        ~A {delete foo; }

protected:
        std::string* foo;
        std::string bar;
}

A myA();
A* otherA = new A();
===========

myA is allocated on the stack, as is myA.bar. myA.foo is on the heap, however, 
and the data that is contained in myA.bar is also most likely on the heap. 
otherA is in the heap, otherA.foo is on the heap, otherA.bar is on the stack 
(though it's data is probably in the heap). objects can have members that are 
on the stack, or on the heap. but each specific object/primitive is only in 
one place or the other. 

the key distinction is the object itself vs class members. this is especially 
important when it comes to classes that share data (e.g. classes that 
implement COW to speed up assignment op and copy ctor).

- -- 
Aaron J. Seigo
GPG Fingerprint: 8B8B 2209 0C6F 7C47 B1EA  EE75 D6B7 2EB1 A7F1 DB43

KDE: The 'K' is for 'kick ass'
http://www.kde.org       http://promo.kde.org/3.1/feature_guide.php
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux)

iD8DBQE+37oq1rcusafx20MRAiVOAJ9UvtvkFV7yCX0iOxXRGIAXtTz1xQCgnp1N
oAGzkJXgzh8Yr7vq4VERiEA=
=HVW/
-----END PGP SIGNATURE-----

Reply via email to