468,768 Members | 1,534 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,768 developers. It's quick & easy.

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 3427
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Dave | last post: by
2 posts views Thread by Johan | last post: by
1 post views Thread by Don Hames | last post: by
2 posts views Thread by Eniac | last post: by
5 posts views Thread by Anjo Gasa | last post: by
7 posts views Thread by John Harrison | last post: by
18 posts views Thread by Goran | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.