lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

>Apparently, this is 
>an issue of realloc(x,0) not working in the same way under 
>different C library implementations. 

I'm very surprised that this is a problem in modern Linuxes. It works fine in
my old Red Hat 5.2. The only system I know that had a problem with realloc(x,0)
was good old Sun OS (not Solaris). (Actually, I think the problem was free(NULL)

>I do not know what the ANSI C standard says about using 
>realloc(x,0). Is it allowed according to the standard to 
>use realloc to free memory? Or is it something undefined 
>or non-canonical?

ANSI C says that realloc(x,0) is the same as free(x) if x!=NULL. The only
murky case occurs with realloc(NULL,0), which is free(NULL), but could be
(wrongly) interpreted as malloc(0)...

Here is what my the page in my RH5.2 says:

       realloc()  changes the size of the memory block pointed to
       by ptr to size bytes.  The contents will be  unchanged  to
       the minimum of the old and new sizes; newly allocated mem-
       ory will be uninitialized.  If ptr is NULL,  the  call  is
       equivalent  to malloc(size); if size is equal to zero, the
       call is equivalent to free(ptr).  Unless ptr is  NULL,  it
       must  have  been  returned by an earlier call to malloc(),
       calloc() or realloc().