How to make whole container to create in heap

S

sandeep

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);
 
M

mlimber

sandeep said:
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-lite/newbie.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-lite/input-output.html#faq-15.1.
void fun()
{

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

Cheers! --M
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

#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-lite/coding-standards.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
 
M

mlimber

Erik said:
Generally not a good idea to use using namespace std, see the FAQ for
more details:
http://www.parashift.com/c++-faq-lite/coding-standards.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
 
N

Noah Roberts

mlimber said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top