I was trying to figure out if there is a portable method for finding out if using "new" succeeded. For example:
int* temp = new int[5000];
If the memory is not allocated for any reason, how can I find out, so I may free some memory up.
Thanx.
I was trying to figure out if there is a portable method for finding out if using "new" succeeded. For example:
int* temp = new int[5000];
If the memory is not allocated for any reason, how can I find out, so I may free some memory up.
Thanx.
straight from the help source...
i got this from bc45's help, and i would assume it's portable, but i wouldn't know for sure...Handling Errors for the new Operator
You can define a function to be called if the new operator fails. To tell the new operator about the new-handler function, use set_new_handler and supply a pointer to the new-handler. If you want new to return null on failure, you must use set_new_handler(0).
hasafraggin shizigishin oppashigger...
The new statement returns a pointer to the allocated location (if it succeeded)
I'd say just check for null (but check the compiler docs) That's pretty easy.
a bad_alloc exception may be thrown by some compilers.
Code:int *ptr; ptr = new int[size]; if( null == ptr ) // memory allocation failure