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 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?
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
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
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
* 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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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;
};
|
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...
|
by: shine |
last post by:
what is the difference between a heap and a stack?
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
|
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...
| |