Memory Allocation Failures with g++

We’ve been having a discussion at work recently about how we should
handle out-of-memory conditions in a C++ app we’re doing under QNX 6.2.
We were (perhaps naively) surprised that, in the following two programs:

// EXAMPLE1
int main( void )
{
char* foo = new char[102410241024];
if( !foo ) printf( “Oops!\n” );

return 0;
}

// EXAMPLE2
int main( void )
{
try { char* foo = new char[102410241024]; }
catch( const std::bad_alloc& ) { printf( “Oops!\n” ); }

return 0;
}


neither one prints “Oops!\n” when run; instead, both cause the app to
abort(). So, we’re sorta waving our hands, wondering what we ought to
do about memory allocation. We don’t really ever expect it it fail.
(We’re not trying to allocate a gigabyte in the actual app.) But…
can we really just say:

char* foo = new char[4096];

and not try to handle an out-of-memory condition? It doesn’t appear
that we can, since gcc 2.95.3 (apparently) won’t let us catch the
bad_alloc exception(?)

Any advice/anecdotes much appreciated…

  • Dave

Which C++ library are you guys using?

chris

David Wolfe <da5id@luvspamwolfe.name> wrote:

We’ve been having a discussion at work recently about how we should
handle out-of-memory conditions in a C++ app we’re doing under QNX 6.2.
We were (perhaps naively) surprised that, in the following two programs:

// EXAMPLE1
int main( void )
{
char* foo = new char[102410241024];
if( !foo ) printf( “Oops!\n” );

return 0;
}

// EXAMPLE2
int main( void )
{
try { char* foo = new char[102410241024]; }
catch( const std::bad_alloc& ) { printf( “Oops!\n” ); }

return 0;
}


neither one prints “Oops!\n” when run; instead, both cause the app to
abort(). So, we’re sorta waving our hands, wondering what we ought to
do about memory allocation. We don’t really ever expect it it fail.
(We’re not trying to allocate a gigabyte in the actual app.) But…
can we really just say:

char* foo = new char[4096];

and not try to handle an out-of-memory condition? It doesn’t appear
that we can, since gcc 2.95.3 (apparently) won’t let us catch the
bad_alloc exception(?)

Any advice/anecdotes much appreciated…

  • Dave

    \


Chris McKillop <cdm@qnx.com> “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

David Wolfe <> da5id@luvspamwolfe.name> > wrote:
We’ve been having a discussion at work recently about how we should
handle out-of-memory conditions in a C++ app we’re doing under QNX 6.2.

“Chris McKillop” <cdm@qnx.com> wrote in message
news:apvgss$cd3$2@nntp.qnx.com

Which C++ library are you guys using?

We’re using whatever comes with Momentics NC/QNX 6.2. (We’re pretty
far along in our evaluation of QNX, but our P.O. for Momentics PE
hasn’t come through Purchasing yet.)

Perhaps it has to do with the way I compiled? I was using this small
program:

// File: mtest.C
#include <stdio.h>

int main( void )
{
try { char* foo = new char[102410241024]; }
catch( const std::bad_alloc& ) { printf( “Oops!\n” ); }

return 0;
}

I just typed:

make mtest

to compile. It built fine, but… is there more to it than that?

David Wolfe wrote:

I just typed:

make mtest

to compile. It built fine, but… is there more to it than that?

Did you notice whether the compiler was invoked with “-fexceptions” ?

I always explicitly enable exceptions, and they work fine for me.

Rennie

try { char* foo = new char[102410241024]; }
catch( const std::bad_alloc& ) { printf( “Oops!\n” ); }

I just typed:

make mtest



Did you notice whether the compiler was invoked with “-fexceptions” ?

No, it was not.

I always explicitly enable exceptions, and they work fine for me.

We have a winner. :sunglasses:

Building with:

make mtest CPPFLAGS=-fexceptions

causes ‘Oops!’ to be printed, as expected. Thanks, Rennie. :slight_smile:

Some things of note.

Better to use qcc and QCC rather then gcc directly. And when (if) you get
PE you can use the dinkum libs which always use exceptions.

chris


David Wolfe <da5id@luvspamwolfe.name> wrote:

try { char* foo = new char[102410241024]; }
catch( const std::bad_alloc& ) { printf( “Oops!\n” ); }

I just typed:

make mtest



Did you notice whether the compiler was invoked with “-fexceptions” ?

No, it was not.

I always explicitly enable exceptions, and they work fine for me.

We have a winner. > :sunglasses:

Building with:

make mtest CPPFLAGS=-fexceptions

causes ‘Oops!’ to be printed, as expected. Thanks, Rennie. > :slight_smile:
\


Chris McKillop <cdm@qnx.com> “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

“Chris McKillop” <cdm@qnx.com> wrote:

Some things of note.

Better to use qcc and QCC rather then gcc directly…

Thanks, Chris. I’ll definitely keep that in mind. Building with:

make mtest CXX=qcc

does seem to do the trick.

David Wolfe <da5id@luvspamwolfe.name> wrote:

“Chris McKillop” <> cdm@qnx.com> > wrote:
Some things of note.

Better to use qcc and QCC rather then gcc directly…

Thanks, Chris. I’ll definitely keep that in mind. Building with:

make mtest CXX=qcc

You will want to actually use…

make mtest CXX=QCC

…otherwise it might get confused when it goes to link. It could still
get confused depending on how you link (you want to make sure it brings
in the C++ libs).

chris


Chris McKillop <cdm@qnx.com> “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/