STL stack initialization

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

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
 
K

Kai-Uwe Bux

Christian said:
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
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top