 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
sandeep Guest
|
Posted: Mon Jun 05, 2006 10:52 pm Post subject: How to make whole container(STL) 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
#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);
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Tue Jun 06, 2006 4:30 am Post subject: Re: How to make whole container(STL) to create in heap |
|
|
"sandeep" <sandeep.k (AT) incore (DOT) in> writes:
| Quote: | 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 ?
|
The object representing the container is wherever you place it. If
it's a local variable, then it's on the stack; if you create it
dynamically, it will be on the heap; and if it is defined at namespace
level or as a static data member, it will be somewhere else.
Where the container object places its internal data (e.g. its
elements) is determined by the allocator the container template is
instantiated with. The default allocator will place the internal data
on the heap.
| Quote: | I am using double linked list so in place of it I want to use STL
#include<stdio.h
#include<iostream
#include<vector
|
You are mixing header generations here. Consistently #including
new-style headers is likely to lead to more predictible results.
| Quote: | using namespace std;
void fun();
void fun1();
vector<int> root;// I want to create this in heap
|
root is defined at namespace level; this means that it is not on the
heap. Since the default allocator is used, the array containing root's
elements will be on the heap, though.
If you want the vector object to be on the heap, you have to create it
with a new expression.
The return type of main() has to be int.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Jeffrey Schwab Guest
|
Posted: Tue Jun 06, 2006 4:31 am Post subject: Re: How to make whole container(STL) to create in heap |
|
|
sandeep wrote:
| Quote: | When we use STL which memory space it will use whither it is stack or
heap or data segment
|
In general, the STL uses both heap and stack space, as well as static
storage. This is true even on architectures that do not support
explicit heap and data segments, or any segmentation at all.
| Quote: | 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 ?
|
The STL containers typically use a small amount of stack space, but use
something called an "allocator" to allocate storage dynamically. E.g.,
a statement like:
std::vector<int> v(1000);
Will create the 1000 elements on the heap, not the stack.
You can instantiate STL types using operator "new," but there is
probably no need. The STL classes are already reasonably smart about
where they store things. If you want fine-grained control over which
heap segments/pages are used, you may want to write a custom allocator.
| Quote: | I am using double linked list so in place of it I want to use STL
#include<stdio.h
|
#include <cstdio>
Is there a reason you are mixing stdio and iostream?
| Quote: | #include<vector
using namespace std;
void fun();
void fun1();
vector<int> root;// I want to create this in heap
|
Why? Do you just mean that you want the ints to be allocated on the heap?
| Quote: | void main()
{
fun();
fun1();
}
void fun1()
{
printf("%d\n",root[1]);
}
void fun()
{
root.push_back(55);
root.push_back(66);
|
These statements both use heap space by default.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Sheff Guest
|
Posted: Tue Jun 06, 2006 4:32 am Post subject: Re: How to make whole container(STL) to create in heap |
|
|
sandeep писал(а):
| Quote: | 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 ?
Hm, I think containers already allocate memory on the heap, in your |
situation
only object itself lies on the stack, so if you want to make ALL
container on the heap, then, I think, you must write something like
this:
vector<int>* root = new vector<int>;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Wed Jun 07, 2006 2:55 am Post subject: Re: How to make whole container(STL) to create in heap |
|
|
Thomas Maeder wrote:
[...]
| Quote: | #include<stdio.h
#include<iostream
#include<vector
You are mixing header generations here.
|
Is he? All of the above headers are defined in the current
standard.
| Quote: | Consistently #including new-style headers is likely to lead to
more predictible results.
|
I don't think so. There are very, very few implementations
where the <cxxx> versions of the C headers are conform. Using
the standard C versions (e.g. <stdio.h>) gives, in practice,
much more predictible results. At least in my experience.
--
James Kanze GABI Software
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Wed Jun 07, 2006 2:56 am Post subject: Re: How to make whole container(STL) to create in heap |
|
|
Jeffrey Schwab wrote:
| Quote: | I am using double linked list so in place of it I want to use STL
#include<stdio.h
#include <cstdio
|
Only if you like non-conformant headers. At least at present,
you're much better off sticking with the C style headers.
| Quote: | #include<iostream
Is there a reason you are mixing stdio and iostream?
|
He doesn't, and the include here is entirely superfluous. On
the other hand, I occasionally include <stdio.h>, even though I
use only iostream for my IO. Where else are functions like
rename, remove or tmpnam declared? (Or the macro BUFSIZ, but
it's a lot less frequent to need that.)
--
James Kanze GABI Software
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|