tags:

views:

265

answers:

3

Hey all,

I am tracking down what might be a memory leak by globally overloading operator new etc... the code to do so compiles fine under VC++, but throws problems in GCC:

15: namespace std { class bad_alloc; };


16: 
17: void * operator new( size_t size ) throw ( std::bad_alloc );

18: void operator delete( void * p ) throw ();

19: void * operator new[]( size_t size ) throw ( std::bad_alloc );

20: void operator delete[]( void * p ) throw ();

The errors that are thrown are:

../zylibcpp/../zylibcpp/utility/MemoryTracker.hpp:17: error: invalid use of incomplete type ‘struct std::bad_alloc’
../zylibcpp/../zylibcpp/utility/MemoryTracker.hpp:15: error: forward declaration of ‘struct std::bad_alloc’
../zylibcpp/../zylibcpp/utility/MemoryTracker.hpp:19: error: invalid use of incomplete type ‘struct std::bad_alloc’
../zylibcpp/../zylibcpp/utility/MemoryTracker.hpp:15: error: forward declaration of ‘struct std::bad_alloc’

What is going on here ?

+1  A: 

The type in the exception specification should be complete. Try including the <memory> <new> header which defines the exception class. The set of headers you include seem to merely declare it, which isn't enough.

Johannes Schaub - litb
+1  A: 

Is this:

namespace std { class bad_alloc; };

in your own code? With a few exceptions (no pun intended) you can't declare things in the std namespace yourself. And the bad_alloc declaration is incomplete.

anon
A: 

The C++ standard (15.4) says that the type in a throw clause must be a complete type (so you can't just forward declare it). It works in MSVC because they break from the standard and say that throw(some_type) is equivalent to throw(...).

Steve M