473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

are container elements on the stack or heap

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

Apr 25 '06 #1
5 2357
* 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?
Apr 25 '06 #2
In article <11**********************@e56g2000cwe.googlegroups .com>,
"Anjo Gasa" <an******@gmail.com> 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
Apr 25 '06 #3
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

Apr 26 '06 #4
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

Apr 26 '06 #5
* Mi*************@tomtom.com:
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?
Apr 26 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
1
by: Geiregat Jonas | last post by:
I'm reading Eric Gunnerson's book. He is talking about the heap and stack, he says you have 2types, value wich are in the stack or inline or reference types wich are in the heap. I don't get...
9
by: shine | last post by:
what is the difference between a heap and a stack?
4
by: Ulrich Hobelmann | last post by:
Hi, how do I correctly instantiate a template container? Everywhere I look they only mention in-place containers, i.e. construction on the stack like "List<Bla> myList;". I tried List<sometype...
4
by: sandeep | last post by:
When we use STL which memory space it will use whither it is stack or heap or data segment How to make STL to create in heap? How to make whole container to create in heap? I think container...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
7
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. ...
18
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.