lua-users home
lua-l archive

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


Thank you for your generous help.

>> >What matters is what the language
>> >standards say, and what they say is that realloc() is permitted to fail
>> >when shrinking a block. (Some realloc implementations never fail in that
>> >case, but many in common use do.)
>> Could you please please give me some references or documents for me to
>> go through?
>> What about malloc(3) provided by the stand C library?
>The realloc function returns a pointer to the new object (which may
>have the same value as a pointer to the old object), or a null
>pointer if the new object could not be allocated.

Thank you for your generous help.
But it still can't answer the question:
Is realloc(provided by stand C library) permitted to fail when
shrinking a block?

Best Regards.
Sunshilong

On Tue, Aug 11, 2020 at 12:34 PM Sean Conner <sean@conman.org> wrote:
>
> It was thus said that the Great 孙世龙 sunshilong once stated:
> > >What matters is what the language
> > >standards say, and what they say is that realloc() is permitted to fail
> > >when shrinking a block. (Some realloc implementations never fail in that
> > >case, but many in common use do.)
> > Could you please please give me some references or documents for me to
> > go through?
> > What about malloc(3) provided by the stand C library?
>
>   Nothing better than the C Standard document itself.  They're pretty easy
> to find on the Internet (I found the C89, C99 and C11 standards).
>
>   But the descriptions are small enough to quote here.  For malloc():
>
>         #include <stdlib.h>
>         void *malloc(size_t size);
>
>         The malloc function allocates spae for an object whose size is
>         speificied by size and whose value in indeterminate.
>
>         The malloc function returns either a null pointer or a pointer to
>         the allocated space.
>
> (wording is the same for C89, C99 and C11).  For free():
>
>         #include <stdlib.h>
>         void free(void *ptr);
>
>         The free function causes the space pointed to by ptr to be
>         deallocated, that is, made available for further allocation.  If ptr
>         is a null pointer, no action occurs.  Otherwise, if the argument
>         does not match a pointer earlier returned by the calloc, malloc, or
>         realloc funciton, or if space as been deallocated by a call to free
>         or realloc, the behavior is undefined.
>
>         The free function returns no value.
>
> (wording is the same for C89, C99 and C11).  And finally realloc()---the
> wording changed between C89 and C99/C11, so first, C89:
>
>         #include <stdlib.h>
>         void *realloc(void *ptr,size_t size);
>
>         The realloc function changes the size of the object pointed to by
>         ptr to the size specified by size.  The contents of the object shall
>         be unchanged up to the lesser of the new and old sizes.  If the new
>         size is larger, the value of the newly allocated portion of the
>         object is indeterminate.  If ptr is a null pointer, the realloc
>         function behaves like the malloc function for the specified size.
>         Otherwise, if ptr does not match a pointer earlier return by calloc,
>         malloc, or realloc function, or if the space has been deallocated by
>         a call to the free or realloc function, the behavior is undefined.
>         If the space cannot be allocated, the object pointed to by ptr is
>         unchanged.  If size is zero and ptr is not a null pointer, the
>         object it points to is freed.
>
>         The realloc function returns either a null pointer or a pointer to
>         the possibly moved allocated space.
>
> and finally C99/C11 (wording is the same for both):
>
>         #include <stdlib.h>
>         void *realloc(void *ptr,size_t size);
>
>         The realloc function deallocates the old object pointed to by ptr
>         and returns a pointer to a new object that has the size specified by
>         size.  The contents of the new object shall be the same as that of
>         the old object prior to deallocation, up to the lesser of the new
>         and old sizes.  Any bytes in the new object beyond the size of the
>         old object have indeterminate values.
>
>         If ptr is a null pointer, the realloc function behhaves like the
>         malloc function for the specfified size.  Otherwise, if ptr does not
>         match a pointer earlier returned by the calloc, malloc, or realloc
>         function, or if the space has deallocated by a call to the free or
>         realloc function, the behavior is undefined.  If memory for the new
>         object cannot be allocated, the old object is not deallocated and
>         its value is unchanged.
>
>         The realloc function returns a pointer to the new object (which may
>         have the same value as a pointer to the old object), or a null
>         pointer if the new object could not be allocated.
>
>   -spc (any typos are mine, it's best to refer to the actual documents)