Can I rely on Tcl_Realloc(NULL) to call Tcl_Alloc()?
R'
It'll work with the implementation as it currently stands, but I'm a
little bit hesitant to say that you can rely on it working. Are we
willing to take on the commitment to keep it working? :-)
Donal.
I see no problem in guaranteeing this behavior going forward.
Jeff
It simplifies code a bit since starting with
char *foo=0;
you can simply say
foo = Tcl_Realloc(foo,len);
instead of
if (foo) foo = Tcl_Realloc(foo,len);
else foo = Tcl_Alloc(len);
R'
As long as you never use a len of 0, yes. The behaviour of code when
faced with a zero-sized alloc is not defined (and the core tries quite
hard to avoid it).
Donal.
But that problem should be the same for Tcl_Alloc(0)...
R'
As an FYI, see this code comment for Tcl_Alloc:
result = TclpAlloc(size);
/*
* Most systems will not alloc(0), instead bumping it to
one so * that NULL isn't returned. Some systems (AIX,
Tru64) will alloc(0) * by returning NULL, so we have to check
that the NULL we get is * not in response to alloc(0).
*
* The ANSI spec
actually says that systems either return NULL *or* * a
special pointer on failure, but we only check for NULL
*/
if ((result == NULL) && size) {
panic("unable to alloc %u bytes", size);
}
return result;
so you can possibly get NULL return if you request a 0-size alloc (or
realloc). This bug has hit Tk before, so watch your code.
Jeff
Documentation updated; it's now part of the specified behaviour.
Donal.