STL stack initialization

Discussion in 'C++' started by Christian Christmann, Aug 25, 2006.

  1. Hi,

    I want to store struct on an STL stack.
    My code:

    struct info {
    list< ClassA* >::const_iterator itBegin;
    list< ClassA* >::const_iterator itEnd;
    bool b;
    int c;
    ClassB *ptrB;
    };

    int foo() {
    ....
    stack< info > myStack;
    ....
    }

    When the program is compiled with gcc 3.4.6 and -O0, everything works fine.
    However, when I compile the program with -O3 and run it, the program execution
    does not finish but runs out of control and consumes all my free RAM. I
    used ddd to debug the program. When the initialization of "myStack" is
    performed, the program enters the STL deque class (ddd tells me that the
    program flow enters stl_deque.h) and there in

    template<typename _Tp, typename _Alloc>
    class _Deque_base

    the constructor

    _Deque_base(const allocator_type& __a, size_t __num_elements)

    is invoked.

    Inside the constructor,

    _M_initialize_map(__num_elements);

    is invoked, where the program loses control (freezing and consuming my entire
    memory).

    Any ideas what is wrong and how I could solve the problem?
    When I omit "ptrB" in the struct, the program works fine.

    Thank you.

    Regards,
    Chris
    Christian Christmann, Aug 25, 2006
    #1
    1. Advertising

  2. Christian Christmann

    Kai-Uwe Bux Guest

    Christian Christmann wrote:

    > Hi,
    >
    > I want to store struct on an STL stack.
    > My code:
    >
    > struct info {
    > list< ClassA* >::const_iterator itBegin;
    > list< ClassA* >::const_iterator itEnd;
    > bool b;
    > int c;
    > ClassB *ptrB;
    > };
    >
    > int foo() {
    > ...
    > stack< info > myStack;
    > ...
    > }
    >
    > When the program is compiled with gcc 3.4.6 and -O0, everything works
    > fine. However, when I compile the program with -O3 and run it, the program
    > execution does not finish but runs out of control and consumes all my free
    > RAM. I used ddd to debug the program. When the initialization of "myStack"
    > is performed, the program enters the STL deque class (ddd tells me that
    > the program flow enters stl_deque.h) and there in
    >
    > template<typename _Tp, typename _Alloc>
    > class _Deque_base
    >
    > the constructor
    >
    > _Deque_base(const allocator_type& __a, size_t __num_elements)


    What is the value of __num_elements here and where does it come from?

    >
    > is invoked.
    >
    > Inside the constructor,
    >
    > _M_initialize_map(__num_elements);


    What is the value of __num_elements here and where does it come from?

    > is invoked, where the program loses control (freezing and consuming my
    > entire memory).
    >
    > Any ideas what is wrong and how I could solve the problem?
    > When I omit "ptrB" in the struct, the program works fine.


    Read the FAQ on how to post a problem. You did not post sufficient code. We
    can only guess.

    Here is my guess: your struct uses raw pointers but does not handle those
    pointers by itself; in particular, it uses the compiler generated default
    constructors, copy constructor, and assignment operator. That can create
    all sorts of problems like unitialized pointers, dangling pointers, double
    deletion of pointers, etc. Given the debugging info, I would think that an
    uninitialized variable could be the culprit. Try valgrind.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Aug 25, 2006
    #2
    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. Surinder Singh
    Replies:
    1
    Views:
    1,175
    Richard Bos
    Dec 20, 2007
  2. Casey Hawthorne
    Replies:
    3
    Views:
    1,064
    Flash Gordon
    Nov 1, 2009
  3. Debajit Adhikary
    Replies:
    36
    Views:
    2,246
    Andre Kaufmann
    Feb 10, 2011
  4. Sam Roberts
    Replies:
    1
    Views:
    212
    Yukihiro Matsumoto
    Feb 11, 2005
  5. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    247
    Kenneth McDonald
    Sep 1, 2007
Loading...

Share This Page