How to make whole container to create in heap

Discussion in 'C++' started by sandeep, Jun 5, 2006.

  1. sandeep

    sandeep Guest

    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);
    sandeep, Jun 5, 2006
    #1
    1. Advertising

  2. sandeep

    mlimber Guest

    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-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
    mlimber, Jun 5, 2006
    #2
    1. Advertising

  3. 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-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
    --
    "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
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jun 5, 2006
    #3
  4. sandeep

    mlimber Guest

    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-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
    mlimber, Jun 5, 2006
    #4
  5. sandeep

    Noah Roberts Guest

    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-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."


    Watch for conflicts if you do that.
    Noah Roberts, Jun 5, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dario Zuffi
    Replies:
    1
    Views:
    581
    David Dorward
    Oct 3, 2005
  2. \A_Michigan_User\
    Replies:
    2
    Views:
    874
    \A_Michigan_User\
    Aug 21, 2006
  3. Michal Slocinski

    Heap dump file size vs heap size

    Michal Slocinski, Mar 25, 2008, in forum: Java
    Replies:
    1
    Views:
    723
    GArlington
    Mar 25, 2008
  4. viki
    Replies:
    6
    Views:
    556
    Erik Wikström
    Jun 28, 2008
  5. Raymond Schanks
    Replies:
    0
    Views:
    504
    Raymond Schanks
    Apr 11, 2010
Loading...

Share This Page