Archived

This topic is now archived and is closed to further replies.

Viscous-Flow

New allocation failue?

Recommended Posts

Viscous-Flow    122
In Microsoft Visual C++ 6.0, does it throw an exception or return a null pointer when new fails? I know according to the new C++ standard it is supposed to throw an exception of BAD_ALLOC (right?), but I have heard that some compilers don''t do this. I have read that you can implicitly make it return a null pointer by doing something like this:
  
char *thing = (nothrow)new char[100000000000000];
  
So, which way is it?

Share this post


Link to post
Share on other sites
Null and Void    1088
MSVC is the only compiler that I know of that doesn''t throw exceptions, and just returns NULL, as the default action. I don''t remember exactly how to make new return NULL, but I think it is something like this:
  
char *thing = new(nothrow) char[100000000000000];


[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
Kwizatz    1392
hmmm funny, I am having a problem with exactlly this kind of thing, I am compiling with MingW32 (Gcc), and never had a problem before with my Map Editor, but yesterday all of the sudden, it started raising an "unknown software exception", I traced it to the line:

nCMFloor = new unsigned short int [nTemp];

the funny thing is that nTemp is the # of floor tiles, which is a filecount, and so far it is 19, not 1000000000000, still it raises the exception, even thoght suposedly GCC doesnt suport exceptions unless you compile with -fexceptions, this might have changed, in any case, I dont see why I am getting this exception, I wasnt getting before and without asking for a 1000000000 allocation,

Any Ideas Void? anyone?

Sorry to post here, its just that since we are already on the topic

thanks

Share this post


Link to post
Share on other sites
Viscous-Flow    122
With all this ambiguity in whether or not to catch throw statements or check if the pointer is null would it just be better to use malloc() and free()?

IS there any significant difference between the C++ way and the C way besides their syntax?

EDIT: Oh ya, I just thought of one, new invokes the constructor

Edited by - Viscous-Flow on November 9, 2001 7:38:12 PM

Share this post


Link to post
Share on other sites
Null and Void    1088
I tested Dev C++, its version of GCC throws exceptions. This is basically what I do (not exactly, but...):
    
#include <exception>

template <class T> inline T *MyNew(void) {
#ifdef _MSC_VER

return new T;
#else

T *New;
try {
New = new T;
} catch(exception) {
return NULL;
}
return New;
#endif

}

Now I use MyNew, and MyNew always returns NULL on failure . It checks for MSVC though. Older Borland compilers have a weird method of catching from new, so it doesn't support them. Now just write another template for new for arrays.

[Resist Windows XP's Invasive Production Activation Technology!]

Edited by - Null and Void on November 9, 2001 7:45:28 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Kwizatz
Any Ideas Void? anyone?



Obviously, your OS shouldn''t complain for so little memory allocation. The problem may be a memory leak. If you often allocate little chunks of memory and forget to release them your application will run out of memory.

Does it crash at beginning or after a certain time ?

Share this post


Link to post
Share on other sites