473,397 Members | 1,961 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,397 software developers and data experts.

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<iostream>
#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",root[1]);
}
void fun()
{

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

Jun 5 '06 #1
4 3671
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<iostream>
#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",root[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<iostream>
#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<iostream>
#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<iostream>
#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
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<>...
2
by: Johan | last post by:
Hi, Where can I find coding example in C++ for homogeneous container. John
7
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...
1
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...
2
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,...
2
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...
5
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,...
7
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()...
18
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
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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...
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.