Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Creating containers on the heap

Reply
Thread Tools

Creating containers on the heap

 
 
DaveJ
Guest
Posts: n/a
 
      09-17-2007
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

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      09-17-2007
DaveJ wrote:
> 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.
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      09-17-2007
On 2007-09-17 12:50, DaveJ wrote:
> 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?


Only if you declared m_VectorOfStrings as std::vector<std::string*>*
i.e. a pointer to a vector of pointers to strings.

> 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.

--
Erik Wikström
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-18-2007
On Sep 17, 1:06 pm, Gianni Mariani <(E-Mail Removed)> wrote:
> DaveJ wrote:
> > 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");


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.)

> > 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.


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.

> > 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.


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.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
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
Are sequence containers not a subset of general containers? Sebastian Mach C++ 5 10-06-2012 07:54 PM
stl heap question: restoring heap validinty after changing 1 element viki C++ 6 06-28-2008 10:12 AM
Heap dump file size vs heap size Michal Slocinski Java 1 03-25-2008 12:54 PM
Containers of iterators vs. containers of references clark.coleman@att.net C++ 7 01-25-2008 01:37 PM
Returning STL containers by value corrupts heap... Sachin Garg C++ 10 09-20-2004 12:29 AM



Advertisments