STL stack initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christian Christmann

    #1

    STL stack initialization

    Hi,

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

    struct info {
    list< ClassA* >::const_iterat or itBegin;
    list< ClassA* >::const_iterat or 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<typena me _Tp, typename _Alloc>
    class _Deque_base

    the constructor

    _Deque_base(con st allocator_type& __a, size_t __num_elements)

    is invoked.

    Inside the constructor,

    _M_initialize_m ap(__num_elemen ts);

    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
  • Kai-Uwe Bux

    #2
    Re: STL stack initialization

    Christian Christmann wrote:
    Hi,
    >
    I want to store struct on an STL stack.
    My code:
    >
    struct info {
    list< ClassA* >::const_iterat or itBegin;
    list< ClassA* >::const_iterat or 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<typena me _Tp, typename _Alloc>
    class _Deque_base
    >
    the constructor
    >
    _Deque_base(con st 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_m ap(__num_elemen ts);
    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

    Comment

    Working...