Archived

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

This topic is 5886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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>
#endif

template <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!]

Share this post


Link to post
Share on other sites
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!]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
};

Share this post


Link to post
Share on other sites