I think that if Stephen is missing the declaration for:

void* operator new(std::size_t size, const std::nothrow_t&) throw();

that it shouldn't be too hard for him to add to some common header file of his. The implementation should be in some runtime library he links with, but if not, adding the following implementation shouldn't cause any problems:

void* operator new (std::size_t size, const std::nothrow_t&) throw()
{
try
{
return operator new (size);
}
catch (...)
{
}

return 0;
}

BTW: I think that "... <new> as this file doesn't seem to exist in the GCC world" as a general statement is false. You may just have to dig deep for it. For instance, on my system, it's at /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/new. Just run "locate include/new" to see where it is on your system.

-- Keith


At 9:51 AM +0100 12/18/02, Henk Jonas wrote:
Isn't

Object* ptr=NULL;

try
{
	ptr = new (xyz);
}
catch (...)
{
}

if (ptr == NULL)
		// kill your idols here

a way to go?

Henk

Stephen Gilfillan wrote:

Hi,

I am using GCC and not CW. I would also like to be able to get new to return NULL
instead of throwing an exception (for subsequent checking).
I'm not able to include <new> as this file doesn't seem to exist
in the GCC world. Consequently 'nothrow' is not recongnised by the GCC compiler.
If anyone has managed to do this using GCC it would be great to hear from you.
I've looked at using the G++ compiler switch "-fcheck-new: - Check that the pointer
returned by operator new is non-null before attempting to modify the storage allocated."
I think I will probably need to use this in any event, but it doesn't solve my problem.
I also compile with "-fno-exceptions" to keep a handle on the size of the prc!

many thanks in advance
Steve

At 10:14 AM +0200 10/18/02, Thomas Damme wrote:
Hello,

I found out, that the operator new just fails and quits the program without any comment if there is not enough space to create an (C++) object.
Can you give me some suggestions how to solve this?
I dont want to wrap every new[] into ErrTry..ErrCatch in my code. A single function that returns the object or NULL would be nice.
Is there some kind wrapper-function to create an object via MemPtrNew() ?
(Anyway, if you think Exceptions are the ultimate way, write me.)
[ General C++ advice, not really Palm-specific ]

new will throw a bad_alloc exception if it is unable to allocate the space.
This is what you are seeing. I suspect that ErrTry & ErrCatch will NOT catch this
behavior, since these are native C++ exceptions, not PalmOS exceptions.

However, there is a 'nothrow' variant of new, which will return NULL instead
of throwing.

Instead of writing: pFoo = new Foo;
you write: pFoo = new (nothrow) Foo;
(yeah, I know it looks strange).

If you use the "nothrow" variation, you need to be sure to include the
standard header <new> first, and unless you add a "using std::nothrow" to
the top of your sourcefile, you'll also need to namespace qualify the item
as "std::nothrow".






_________________________________________________________________
The new MSN 8: advanced junk mail protection and 2 months FREE* http://join.msn.com/?page=features/junkmail



--
-------------------------------------------------------------------------
[EMAIL PROTECTED] www.metaviewsoft.de

<A HREF="http://www.handango.com/PlatformTopSoftware.jsp?authorId=95946";>
<IMG SRC="http://user.cs.tu-berlin.de/~jonash/werbung_palmos.jpg";></A>
-------------------------------------------------------------------------


--
For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/

--
For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/

Reply via email to