473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

STL stack initialization

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
Aug 25 '06 #1
1 2739
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

Aug 25 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
8
by: Ben | last post by:
Hi all, I implemented a stack in C++ in 2 different ways and I'd like to know which approach is better than the other.. and if there is any difference between the two? I'd also like to know if...
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
41
by: Nitin Bhardwaj | last post by:
Hi all, I wanted to know whether the stack in a C program is growing upwards or downwards.So I wrote a little code to see that.Please guide me as to whether this code is correct in telling this...
2
by: ronjon | last post by:
I am trying to corelate C code and its associated assembly code produced by gcc. In particular,I am trying to figure out how the stack pointer increments during variable initialization inside a...
148
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
20
by: merrittr | last post by:
I need some C advice I want to read in string commands from a user when the user enters a \n I want to push it on the stac. Then at some point , if the user enters the word print pop off and print...
2
by: =?Utf-8?B?VG9tIEFsbGVu?= | last post by:
Hi, TIA for any help. (Thanks, Mattias Sjögren for your help yesterday! ) I am developing a C# wrapper for a 3rd party API. It processes buffers from a video stream (file) and is structured...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.