473,545 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make whole container to create in heap

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 uses stack is it correct ?

I am using double linked list so in place of it I want to use STL for

#include<stdio. h>
#include<iostre am>
#include<vector >
using namespace std;
void fun();
void fun1();
vector<int> root;// I want to create this in heap
void main()
{
fun();
fun1();

}
void fun1()
{
printf("%d\n",r oot[1]);
}
void fun()
{

root.push_back( 55);
root.push_back( 66);

Jun 5 '06 #1
4 3682
sandeep wrote:
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 uses stack is it correct ?
Technically, Standard C++ doesn't discuss heap vs. stack. It refers to
"automatic storage" (which may be on the stack) and "free store" (which
may be a heap or heaps), and it allows compiler implementors to do
things differently for particular architectures in which, e.g., a stack
is not the most efficient implementation.

That being said, all containers use the free store for their containees
(unless you create an allocator to do differently), though the
container itself (e.g., head and tail pointers for std::list) might be
either automatic or dynamically allocated. Consider:

#include <vector>

void Foo()
{
std::vector<int > v1;
std::vector<int >* v2 = new std::vector<int >();
// ...
delete v2;
}

Here v1 and v2 will allocate their data on the free store, but the
housekeeping elements of v1 are automatic (in your case, on the stack)
while those of v2 are on the free store (though the pointer v2 itself
is still an automatic object).
I am using double linked list so in place of it I want to use STL for
Is this a completely separate question?

#include<stdio. h>
#include<iostre am>
#include<vector >
using namespace std;
void fun();
void fun1();
vector<int> root;// I want to create this in heap
First, this is not a doubly linked list. std::vector is essentially a
smart array, which means that its insertions and deletions are
expensive. If you need a linked list, you might consider std::list
(compare http://www.sgi.com/tech/stl/List.html).

Second, don't use global variables. Reducing variable scope as much as
possible helps make programs more understandable and robust.

May I suggest you get a good C++ book such as _Accelerated C++_ by
Koenig and Moo?
void main()
int main(). See
http://www.parashift.com/c++-faq-lit....html#faq-29.3.
{
fun();
fun1();

}
void fun1()
{
printf("%d\n",r oot[1]);
}
Prefer iostreams for type safety, etc. See
http://www.parashift.com/c++-faq-lit....html#faq-15.1.
void fun()
{

root.push_back( 55);
root.push_back( 66);

}

Cheers! --M

Jun 5 '06 #2
On 2006-06-05 13:34, sandeep wrote:

#include<stdio. h>
#include<iostre am>
#include<vector >
using namespace std;
Generally not a good idea to use using namespace std, see the FAQ for
more details:
http://www.parashift.com/c++-faq-lit....html#faq-27.5
void fun();
void fun1();
vector<int> root;// I want to create this in heap


Not going to answer your question since mlimber have already done so,
but would just like to point out that you have managed to declare your
vector in such a way that most implementations/architectures would place
it neither on the stack nor the heap. Global variables and static
variables are special since they exist for the whole duration of the
application and are thus normally stored in their own area of memory.

Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
Jun 5 '06 #3
Erik Wikström wrote:
On 2006-06-05 13:34, sandeep wrote:

#include<stdio. h>
#include<iostre am>
#include<vector >
using namespace std;


Generally not a good idea to use using namespace std, see the FAQ for
more details:
http://www.parashift.com/c++-faq-lit....html#faq-27.5


Note, however, that there are other schools of thought. For instance,
Sutter and Alexandrescu say in _C++ Coding Standards_, item 59 (italics
in original): "You can and should use namesapce using declarations and
directives liberally /in your implementation files after #include
directives/ and feel good about it. Despite repeated assertions to the
contrary, namespace using declarations and directives are not evil and
they do not defeat the purposes of namespaces. Rather, they are what
make namespaces usable."

Cheers! --M

Jun 5 '06 #4

mlimber wrote:
Erik Wikström wrote:
On 2006-06-05 13:34, sandeep wrote:

#include<stdio. h>
#include<iostre am>
#include<vector >
using namespace std;


Generally not a good idea to use using namespace std, see the FAQ for
more details:
http://www.parashift.com/c++-faq-lit....html#faq-27.5


Note, however, that there are other schools of thought. For instance,
Sutter and Alexandrescu say in _C++ Coding Standards_, item 59 (italics
in original): "You can and should use namesapce using declarations and
directives liberally /in your implementation files after #include
directives/ and feel good about it. Despite repeated assertions to the
contrary, namespace using declarations and directives are not evil and
they do not defeat the purposes of namespaces. Rather, they are what
make namespaces usable."


Watch for conflicts if you do that.

Jun 5 '06 #5

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

Similar topics

7
3111
by: Dave | last post by:
Hello all, I'm pondering why the default underlying container for std::priority_queue<> is std::vector<>. It would seem that inserts are liable to happen anywhere, which would make std::list<> a superior alternative. Why factors am I not considering here? Why in the general case would std::vector<> be best? Thanks, Dave
2
2362
by: Johan | last post by:
Hi, Where can I find coding example in C++ for homogeneous container. John
7
3157
by: Irish | last post by:
Hello all :) Hopefully someone can shed some light on this problem I'm having. I'm trying to declare a variable to a type of class I've defined (which is a minHeap), but actually instantiate it later when I get a command from the user. This is my class and if you look down a lil you can see what I'm trying to do in my main(). I want to ask...
1
3360
by: Don Hames | last post by:
I have a windows application that has a split container in the client area. In the left panel, I added controls via the designer in VS 2005. In the right panel, I want to dynamically create and display other forms that I created in the project. Is this possible? Can the panel2 of a split container be the container for a dynamically created...
2
1290
by: Saber | last post by:
Public Class User Private _id, _P, _L, _R As Integer Private _balance As Int64 Private _name, _family, _tel1, _tel2, _mobile, _city, _accountNumber As String Private _password, _email, _ficheNumber, _postalAddress As String Private _province, _bank As String Private _isSpecial, _photo As Boolean Public stk As New Stack
2
2273
by: Eniac | last post by:
*argh* ... *pull hairs* I've recently started developing from ASP to ASP.NET The switch was fairly smooth since i had done some VB.net before ... then came...FORMS! :) I find it astounding at how difficult it has become to control a form, something that was so dead easy in ASP.
5
2372
by: Anjo Gasa | last post by:
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 );
7
1625
by: John Harrison | last post by:
This is from SGI's FAQ, its the justification for why list<T>::size() is linear time in their library (and in gcc library too since their code is based on SGI) <quote> Why is list<>::size() linear time? The size() member function, for list and slist, takes time proportional to the number of elements in the list. This was a deliberate...
18
1799
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
7484
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7415
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7675
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7928
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7440
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7775
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
726
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.