New

Started by
8 comments, last by Viscous-Flow 22 years, 5 months ago
To see if something allocated memory using new would this work:
        
//  Something to this effect

if(memAllocated == NULL)
 return MEMORY_ALLOCATION_FAILURE;
        
I know new throws an exception, but does it make what you were trying to allocated memory for NULL? I tried my theory out by creating some odd 30 instances of IE Explorer and executed my program with: 0% System Resources 2% User Resources 0% GDI Resources And the program did generate an error. I guess that form of memory allocation check worked. Am I right? [EDIT]: I know that malloc makes the variable NULL does new? [EDIT]: Aargh, spelin miztacs Edited by - Viscous-Flow on October 29, 2001 8:27:15 PM Edited by - Viscous-Flow on October 29, 2001 8:28:02 PM
Advertisement
New is suppossed to throw an exception, but it doesn''t in MSVC (it does in Borland). MSVC just returns NULL if it fails. I don''t know about GCC, I haven''t tried.

[Resist Windows XP''s Invasive Production Activation Technology!]
GCC also throws an exception, but doesn''t return NULL.

[Resist Windows XP''s Invasive Production Activation Technology!]
Thanks, they sure need to come to a consensus, eh?
I wrote a header like this to do new for me, that way it will be accurate under MSVC as well as other compilers (also, it is really nice for finding memory leaks ). It doesn''t work under older Borland''s, unfortunetly. You can tell from the #if that I''m expecting newer MSVC''s to actually throw exceptions...
  #ifndef INC_RUNE_0016xx_MASTER_COMMON_MEM_HEADER#define INC_RUNE_0016xx_MASTER_COMMON_MEM_HEADER#if (!defined(_MSC_VER) || _MSC_VER > 0x000004B0)	#define NEW_USES_EXCEPTIONS	#include <exception>#endiftemplate <class T> inline T *rNew(void) {	#ifdef NEW_USES_EXCEPTIONS		T *Get;		try {			Get = new T;		} catch(exception) {			return NULL;		}		return Get;	#else		return new T;	#endif}template <class T> inline T *rNew(unsigned int Count) {	#ifdef NEW_USES_EXCEPTIONS		T *Get;		try {			Get = new T[Count];		} catch(exception) {			return NULL;		}		return Get;	#else		return new T[Count];	#endif}template <class T> inline void rDelete(T *What) {	delete What;}template <class T> inline void rDeleteArr(T *What) {	delete [] What;}#ifdef NEW_USES_EXCEPTIONS	#undef NEW_USES_EXCEPTIONS#endif#endif  



[Resist Windows XP''s Invasive Production Activation Technology!]
Ok, that''s the most my code has ever been mangled by this board, a new record!

[Resist Windows XP''s Invasive Production Activation Technology!]
quote:Original post by Null and Void
Ok, that''s the most my code has ever been mangled by this board, a new record!


Well, edit it then.
quote:Original post by Darkor
Well, edit it then.

Would that help? No, I don''t think so. The code I pasted looked fine, it is their script with warps it. You paste some code here, and see what happens to it if it''s in the source tags.

[Resist Windows XP''s Invasive Production Activation Technology!]
quote:Original post by Null and Void
Would that help? No, I don''t think so. The code I pasted looked fine, it is their script with warps it. You paste some code here, and see what happens to it if it''s in the source tags.


You have to insert some extra blank lines, or lines with only whitspace in them, for the source-boxes to look right. In particular most preprocessor directives usually make their terminating newline disappear, but having an extra blank line after every #include, #ifdef, etc. does the trick.
In reference to the original question: There is a version of new that is guaranteed to *not* throw any exception if it fails, and simply return NULL instead. To use it pass std::nothrow as a parameter to new.

e.g.
  using std::nothrow;void foobar(){   int *ptr = new (nothrow) int;};  

This topic is closed to new replies.

Advertisement