THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 01/04/2012 at 03:00, xxxxxxxx wrote:
Originally posted by xxxxxxxx
@jack: Just a note, returning allocated memory from a function is considered bad. It's also some kind of confusing (imho).
If it was considered bad, I don't think we would have any Alloc() functions in the API...
BaseObject *op = BaseObject::Alloc(Onull);
In fact, even the memory allocation is done by a function:
LONG* myarray = GeAllocType(LONG, count);
And yes, it might be confusing. But programming is a confusing thing if one does not take care ;-) Therefore, we wouldn't call this function "foo" but rather something with "Alloc".
Originally posted by xxxxxxxx
if (arr) GeFree(arr);The conditional is not necessary. If 'arr' is NULL then GeFree doesn't do anything (and it won't crash). You've already checked that arr isn't NULL so it should be doubly okay.
Right, in this example it was obvious. But it'S better to have double and triple checks than to run into some surprising case (in more complex code) and have it crash on the user's machine.
Originally posted by xxxxxxxx
One case to remember is that if you free memory and the execution continues you should set the pointer to it equal to NULL.
With GeFree and also with the static Free methods of the C4D API classes, that is not necessary. They set your pointer to NULL automatically (pointer reference (void*& p)). Same with the gDelete and bDelete macros.
But generally, of course, one should take care to set the pointer to NULL when any other method of memory freeing is used.