473,399 Members | 2,478 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,399 software developers and data experts.

Creating containers on the heap

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

Sep 17 '07 #1
3 3530
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.
Sep 17 '07 #2
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
Sep 17 '07 #3
On Sep 17, 1:06 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
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:ja*********@gmail.com
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

Sep 18 '07 #4

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

Similar topics

10
by: Sachin Garg | last post by:
Hi, When trying to return objects of type std::list<MyClass> from my function, I get a corrupt heap. (I checked using MSVC++ 7.0 runtime heap check facility) Basically, I am creating a...
4
by: Andy Venikov | last post by:
Is there a way to change standard containers' behavior to expect a 0 when allocation fails (same way you can change operator new's behavior to return null by passing std::nothrow) ? In my...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
19
by: AlesD | last post by:
Hello, I have problem that when I use std::list<MyClassand then store various subclasses of MyClass in that list (or any other STL container) the instances get sliced. I have read FAQ: ' What...
10
by: rAinDeEr | last post by:
Hi, I am trying to create around 70 tablespaces for around 100 tables.. Am using DB2 UDB 8.2 in Linux environment... This is one i generated through Control centre.... CREATE REGULAR...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
13
by: Justcallmedrago | last post by:
How would you declare and assign a variable inside a function THAT HAS THE NAME OF A PARAMETER YOU PASSED example: when you call createvariable("myvariable") it will declare the variable...
11
by: jimxoch | last post by:
Hi list, Most STL containers are storing their data on the heap. (although some std::string implementations are notable exceptions) Of course, using the heap as storage increases flexibility and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.