On Mon, 1 Sep 2025 17:19:52 +0200
Philipp Klaus Krause <p...@spth.de> wrote:

> void *realloc(void *ptr, size-t size);
> 
> C90: "[…] 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."
> 
> C99TC2 (also C99TC3 and C11): "[…] If memory for the new object cannot 
> be allocated, the old object is not deallocated and its value is unchanged."
> 
> C17: "If size is zero and memory for the new object is not allocated, it 
> is implementation-defined whether the old object is deallocated. If the 
> old object is not deallocated, its value shall be unchanged."
> 
> C23 (same in current C2Y draft): "[…] if the size is zero, the behavior 
> is undefined. If memory for the new object is not allocated, the old 
> object is not deallocated and its value is unchanged."
> 
> Currently, our malloc and realloc cannot allocate an object of size 
> zero, they return NULL on such a request, and realloc does free. To me 
> that looks compliant with C90, C17 and C23, but not C99.
> 
> I would like to change that to allowing allocation of zero-sized objects 
> instead. Then, malloc(0) and realloc(ptr, 0) would return a non-null 
> pointer. This would be compliant with C99, C17 and C23, but not C90.

I would disagree with your interpretation re C90. It's possible to
allocate zero bytes, it's just metadata tracking space that is allocated
but it's not an impossible act. So I believe you are compliant with C90
anyway. They just made it more explicit.

Classic Unix returned a pointer for zero sized allocations and realloc.

Alan


_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to