Skip to main content
GameDev.net gamedev.net
🔒 Locked

Getting new(std::nothrow) to work?

Started by OrangyTang May 12, 2003 at 8:29 AM 2 replies 1.8k views
Original Post
OrangyTang
OrangyTang

  
// Old code:

MyClass* pMyClass = new MyClass;
// use pointer..


// non-exception code:

#include <new>

MyClass* pMyClass = new(std::nothrow) MyClass;
if (pMyClass != NULL)
    // use pointer..   
Annoyingly, the second version will refuse to compile, regardless of various methods, including a new (sizeof(MyClass), std::nothrow) version, and a few more for good luck. I just get a: error C2665: 'new' : none of the 3 overloads can convert parameter 2 from type 'const struct std::nothrow_t' All the time Anyone know the cause? This is with Visual C++ 6 enterprise ed. Anyone know a suitable workaround (assuming a C-style no-exception based way of doing things). Thanks. [edited by - OrangyTang on May 12, 2003 9:30:44 AM]
noVum
noVum
AFAIK the VC++ 6.0 version of new never throws an exception and will return 0 if you get a bad allocation

quote:

Beginning in Visual C++ .NET (version 7 of the compiler), the CRT''s new function (in libc.lib, libcd.lib, libcmt.lib, libcmtd.lib, msvcrt.lib, and msvcrtd.lib) will continue to return NULL if memory allocation fails. However, the new function in the Standard C++ Library (in libcp.lib, libcpd.lib, libcpmt.lib, libcpmtd.lib, msvcprt.lib, and msvcprtd.lib) will support the behavior specified in the C++ standard, which is to throw a std::bad_alloc exception if the memory allocation fails.

Qw3r7yU10p!
Qw3r7yU10p!
works fine for me, same setup with SP4

try rebuilding (c:

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.