What's the name of that one function...?

Started by
8 comments, last by silvermace 19 years, 5 months ago
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).
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
Advertisement
What language?

In C++ when memory allocation fails, a std::bad_alloc exception is raised.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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...
[size="2"]I like the Walrus best.
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) ?
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
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}
FTA, my 2D futuristic action MMORPG
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...
if hes using new, wouldnt he need to use an exception here ?
FTA, my 2D futuristic action MMORPG
you're looking for assert, heres an example
int* p = new int[10];assert(p != NULL);


Quote:MSDN Online
If unsuccessful, by default new returns zero.
by default, new returns a NULL pointer (zero to be exact)
EDIT: MSDN check-up
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
I didn't believe Microsoft would bork up something as simple as that, so I checked:

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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
i conceed. bloody microsoft.
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website

This topic is closed to new replies.

Advertisement