Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > are container elements on the stack or heap

Reply
Thread Tools

are container elements on the stack or heap

 
 
Anjo Gasa
Guest
Posts: n/a
 
      04-25-2006
Let's say I have:

std::vector<Object> rgObjects;

rgObjects itself is declared as a local variable and hence on the
stack. But what about as I add elements to rgObjects:

Object newObject( 3, 4 );
rgObjects.push_back( newObject );

newObject itself isn't placed on the vector, rather the copy
constructor is called, and an object copied with its values is placed
on the vector. Does this new instance of Object reside on the stack or
the heap.

Anjo

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-25-2006
* Anjo Gasa:
> Let's say I have:
>
> std::vector<Object> rgObjects;
>
> rgObjects itself is declared as a local variable and hence on the
> stack. But what about as I add elements to rgObjects:
>
> Object newObject( 3, 4 );
> rgObjects.push_back( newObject );
>
> newObject itself isn't placed on the vector, rather the copy
> constructor is called, and an object copied with its values is placed
> on the vector. Does this new instance of Object reside on the stack or
> the heap.


C++ has no notion of stack or heap: that's an implementation aspect.

That said, the copy might reside in dynamically allocated memory or not,
depending on the implementation of std::vector, the current size of the
vector, and e.g. the phase of the moon.

Why do you want to know?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      04-25-2006
In article <(E-Mail Removed) .com>,
"Anjo Gasa" <(E-Mail Removed)> wrote:

> Let's say I have:
>
> std::vector<Object> rgObjects;
>
> rgObjects itself is declared as a local variable and hence on the
> stack. But what about as I add elements to rgObjects:
>
> Object newObject( 3, 4 );
> rgObjects.push_back( newObject );
>
> newObject itself isn't placed on the vector, rather the copy
> constructor is called, and an object copied with its values is placed
> on the vector. Does this new instance of Object reside on the stack or
> the heap.
>
> Anjo


The heap.

-Howard
 
Reply With Quote
 
noone
Guest
Posts: n/a
 
      04-26-2006
On Tue, 25 Apr 2006 07:54:51 -0700, Anjo Gasa wrote:

> Let's say I have:
>
> std::vector<Object> rgObjects;
>
> rgObjects itself is declared as a local variable and hence on the stack.
> But what about as I add elements to rgObjects:
>
> Object newObject( 3, 4 );
> rgObjects.push_back( newObject );
>
> newObject itself isn't placed on the vector, rather the copy constructor
> is called, and an object copied with its values is placed on the vector.
> Does this new instance of Object reside on the stack or the heap.


AFAIK, the only requirement to the vector is that its objects occupy
consecutive memory locations so that accessing the elements as an array is
possible. Think about what you're asking though. What is the typical
size of a stack frame compared to the free memory (heap). Why would
anyone implement such a generic container to use stack space? There may
be some special implementation where the space is on the stack but I don't
think they are too common.

-noone of consequence

 
Reply With Quote
 
Michiel.Salters@tomtom.com
Guest
Posts: n/a
 
      04-26-2006
Alf P. Steinbach wrote:
> * Anjo Gasa:
> > Let's say I have:
> >
> > std::vector<Object> rgObjects;
> >
> > rgObjects itself is declared as a local variable and hence on the
> > stack. But what about as I add elements to rgObjects:
> >
> > Object newObject( 3, 4 );
> > rgObjects.push_back( newObject );
> >
> > newObject itself isn't placed on the vector, rather the copy
> > constructor is called, and an object copied with its values is placed
> > on the vector. Does this new instance of Object reside on the stack or
> > the heap.

>
> ... the copy might reside in dynamically allocated memory or not,
> depending on the implementation of std::vector, the current size of the
> vector, and e.g. the phase of the moon.


I don't think the default allocator has such liberty (and Anjo didn't
override it)
so the copy will reside in memory dynamically allocated by the default
allocator.

HTH,
Michiel Salters

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-26-2006
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> Alf P. Steinbach wrote:
>> * Anjo Gasa:
>>> Let's say I have:
>>>
>>> std::vector<Object> rgObjects;
>>>
>>> rgObjects itself is declared as a local variable and hence on the
>>> stack. But what about as I add elements to rgObjects:
>>>
>>> Object newObject( 3, 4 );
>>> rgObjects.push_back( newObject );
>>>
>>> newObject itself isn't placed on the vector, rather the copy
>>> constructor is called, and an object copied with its values is placed
>>> on the vector. Does this new instance of Object reside on the stack or
>>> the heap.

>> ... the copy might reside in dynamically allocated memory or not,
>> depending on the implementation of std::vector, the current size of the
>> vector, and e.g. the phase of the moon.

>
> I don't think the default allocator has such liberty (and Anjo didn't
> override it)
> so the copy will reside in memory dynamically allocated by the default
> allocator.


That's news to me.

It would mean that the standard disallows small-size optimization using
a fixed buffer, or that there is a fundamental difference in those
requirements for std::basic_string and std::vector (apart from
contigousness, which now is solved).

I don't think there is such a difference -- and if there is, it would
be a defect in the standard.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
number of distinct elements in a const container and iterating over the distinct elements Hicham Mouline C++ 1 04-11-2010 10:56 AM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 1 01-30-2004 04:09 PM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 0 01-28-2004 02:50 PM



Advertisments