tech-userlevel archive

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

Re: reallocarr(3)



Joerg,

Right "type *a = NULL; reallocarr(&a, 1, 2);" works.
I was confused with the pointer magic.

Christos Zoulas wrote
> Kamil Rytarowski <n54%gmx.com@localhost> wrote:
> >Joerg Sonnenberger wrote:
> >
> >reallocarr(3):
> >+ distinguish ENOMEM and EOVERFLOW
> >+ tries to preserve global errno
> 
> That's not the only problem. What does reallocarray(ptr, 10, 0)
> return in the current OpenBSD implementation? What should it return?
> How about reallocarray(ptr, 0, 10)?

In C this is legal, but implementation defined. GNU C afair assumes malloc(1).
For a safe function I propose to trigger ENOTSUP.

> When reallocarray returns NULL,
> can one check errno to determine if the allocation failed?

Yes ENOMEM, from malloc(3):

The malloc() and calloc() functions return a pointer to the allocated
memory if successful; otherwise a NULL pointer is returned and errno is
set to ENOMEM.

The realloc() function returns a pointer, possibly identical to ptr, to
the allocated memory if successful; otherwise a NULL pointer is returned,
and errno is set to ENOMEM if the error was the result of an allocation
failure.  The realloc() function always leaves the original buffer intact
when an error occurs.

> 
> christos
> 
> 

I propose to add EOVERFLOW for overflow and ENOTSUP for a 0-sized allocation.


Home | Main Index | Thread Index | Old Index