What's the name of that one function...?
I remember that, when you dynamically allocate memory, there is that one function that checks if the operation failed or not. If I remember correctly, it was in its own separate include file...
(if that isn't enough, only thing that comes to my mind is "assert" but no that doesn't seem to be it).
assert (pointer) ?
The ANSI assert macro is typically used to identify logic errors during program development by implementing the expression argument to evaluate to false only when the program is operating incorrectly.[...]assert prints a diagnostic message when expression evaluates to false (0) and calls abort to terminate program execution. No action is taken if expression is true (nonzero).
EDIT:
Look at that, there is an article right here on gamedev about this stuff...
The ANSI assert macro is typically used to identify logic errors during program development by implementing the expression argument to evaluate to false only when the program is operating incorrectly.[...]assert prints a diagnostic message when expression evaluates to false (0) and calls abort to terminate program execution. No action is taken if expression is true (nonzero).
EDIT:
Look at that, there is an article right here on gamedev about this stuff...
Quote:Original post by Fruny
What language?
In C++ when memory allocation fails, a std::bad_alloc exception is raised.
uh-huh.... and how would I catch it? just catch(bad_alloc) ?
not an exception expert, in fact i dont use them (sorry please dont yell). anyway, i'd assume it would look like this:
try{ Blah *b = new Blah();}catch(std::bad_alloc ba){ //do stuff here to save the day}
if you use malloc to allocate your memory and the operation failed, a NULL is actually returned.
Therefore you don't need "another function" to check if the operation fails.
assert() is a great way to catch errors under debug mode. But use if()...for general purposes.
I dont see the need of try{}catch{} here...
Therefore you don't need "another function" to check if the operation fails.
assert() is a great way to catch errors under debug mode. But use if()...for general purposes.
I dont see the need of try{}catch{} here...
you're looking for assert, heres an example
EDIT: MSDN check-up
int* p = new int[10];assert(p != NULL);
Quote:MSDN Onlineby default, new returns a NULL pointer (zero to be exact)
If unsuccessful, by default new returns zero.
EDIT: MSDN check-up
I didn't believe Microsoft would bork up something as simple as that, so I checked:
They do in fact do it right, and you're wrong. Who would have thunk it. You should know better than to go against whatever Fruny says, anyway.
And assert is for checking for things that should be impossible, not things that can happen rarely that you don't want to deal with.
Quote:From MSDN Online
Beginning in Visual C++ .NET 2002, 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.
They do in fact do it right, and you're wrong. Who would have thunk it. You should know better than to go against whatever Fruny says, anyway.
And assert is for checking for things that should be impossible, not things that can happen rarely that you don't want to deal with.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement