Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tcl_Realloc(NULL,len)?

18 views
Skip to first unread message

Ralf Fassel

unread,
Jun 1, 2006, 7:02:34 AM6/1/06
to
The manpage says nothing about calling Tcl_Realloc() with a NULL
pointer. Current code in 8.4.11 (and most probably in all other TCL
versions?) simply calls Tcl_Alloc(len) when the pointer argument is
NULL (this resembles realloc(3) behaviour which has it documented).

Can I rely on Tcl_Realloc(NULL) to call Tcl_Alloc()?

R'

Donal K. Fellows

unread,
Jun 1, 2006, 11:21:36 AM6/1/06
to
Ralf Fassel wrote:
> Can I rely on Tcl_Realloc(NULL) to call Tcl_Alloc()?

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.

Jeff Hobbs

unread,
Jun 1, 2006, 12:05:59 PM6/1/06
to Donal K. Fellows

I see no problem in guaranteeing this behavior going forward.

Jeff

Ralf Fassel

unread,
Jun 1, 2006, 12:14:39 PM6/1/06
to
* Jeff Hobbs <je...@activestate.com>
| > Ralf Fassel wrote:
--<snip-snip>--

| I see no problem in guaranteeing this behavior going forward.

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'

Donal K. Fellows

unread,
Jun 2, 2006, 8:58:39 AM6/2/06
to
Ralf Fassel wrote:
> It simplifies code a bit since starting with
> char *foo=0;
> you can simply say
> foo = Tcl_Realloc(foo,len);

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.

Ralf Fassel

unread,
Jun 2, 2006, 2:00:47 PM6/2/06
to
* "Donal K. Fellows" <donal.k...@manchester.ac.uk>

| > you can simply say
| > foo = Tcl_Realloc(foo,len);
|
| As long as you never use a len of 0, yes.

But that problem should be the same for Tcl_Alloc(0)...

R'

Jeff Hobbs

unread,
Jun 4, 2006, 4:03:13 PM6/4/06
to

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

Donal K. Fellows

unread,
Jun 5, 2006, 6:30:37 AM6/5/06
to
Jeff Hobbs wrote:
> I see no problem in guaranteeing this behavior going forward.

Documentation updated; it's now part of the specified behaviour.

Donal.

0 new messages