Creating containers on the heap

D

DaveJ

Hi,

This is quite a simple question (hopefully).

If I create a vector (on any container) on the heap e.g.
std::vector<std::string> * m_VectorOfStrings = new
vector<std::string>;

I know that the vector itself will be placed on the heap, but if I
just add strings to it such as:
m_VectorOfStrings.push_back("mystring1");

Will the string also be stored on the heap? Or do I need to create a
string with the new operator as well?
I assummed that any data stored inside a container on the heap would
also be stored on the heap, but just wanted to clarify this.


Thanks
 
G

Gianni Mariani

DaveJ said:
Hi,

This is quite a simple question (hopefully).

If I create a vector (on any container) on the heap e.g.
std::vector<std::string> * m_VectorOfStrings = new
vector<std::string>;

I know that the vector itself will be placed on the heap, but if I
just add strings to it such as:
m_VectorOfStrings.push_back("mystring1");

Will the string also be stored on the heap? Or do I need to create a
string with the new operator as well?

The string will be allocated using the default allocator for
std::basic_string, which more than likely uses heap.
I assummed that any data stored inside a container on the heap would
also be stored on the heap, but just wanted to clarify this.

They are independant.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi,

This is quite a simple question (hopefully).

If I create a vector (on any container) on the heap e.g.
std::vector<std::string> * m_VectorOfStrings = new
vector<std::string>;

I know that the vector itself will be placed on the heap, but if I
just add strings to it such as:
m_VectorOfStrings.push_back("mystring1");
Will the string also be stored on the heap?

The string will be on the heap.
Or do I need to create a string with the new operator as well?

I assummed that any data stored inside a container on the heap would
also be stored on the heap, but just wanted to clarify this.

Unless you do some funny business with allocators all elements in the
standard containers will be on the heap, regardless if the container is
on the heap or not.
 
J

James Kanze

Note that this whould be:
m_VectorOfStrings->push_back("mystring1");
(I can't quite see why one would ever allocate a vector member
dynamically, however.)
The string will be allocated using the default allocator for
std::basic_string, which more than likely uses heap.

The strings themselves will be allocated using the allocator of
the vector (which by default uses global operator new). Any
additional dynamic memory needed by the string will be allocated
by the allocator of string.
They are independant.

More to the point, of course: the container manages any memory
it might need. It's not really your problem, unless you want to
play around with allocators.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top