Handling argc==0 in the kernel
Handling argc==0 in the kernel
Posted Jan 30, 2022 20:21 UTC (Sun) by NYKevin (subscriber, #129325)In reply to: Handling argc==0 in the kernel by larkey
Parent article: Handling argc==0 in the kernel
In practice, most implementations either return NULL or convert it into a call to malloc(1). But even then, that's a one-byte array, not a one-object array, so you still can't legally add one to it unless the pointer's type has sizeof() == 1.
Posted Jan 30, 2022 20:23 UTC (Sun)
by NYKevin (subscriber, #129325)
[Link]
Or realloc(), I suppose. Point is, you definitely can't dereference it.
Posted Jan 31, 2022 1:20 UTC (Mon)
by jreiser (subscriber, #11027)
[Link] (2 responses)
One of the uses of malloc(0) is to serve as an analogue of the Lisp function (gensym): return a unique value, one that does not point to anything else returned by malloc, both now and for the remaining life of the process. (Yes, the standard requires this.) An implementation for which malloc(0) always returns NULL is not acceptable. So in practice, many implementations begin with something like size += (0==size), i.e. malloc(0) is treated as malloc(1).
Posted Jan 31, 2022 1:31 UTC (Mon)
by ABCD (subscriber, #53650)
[Link]
An implementation for which malloc(0) always returns NULL is expressly permitted by the POSIX standard:
If the size of the space requested is 0, the behavior is implementation-defined: the value returned shall be either a null pointer or a unique pointer.
The C17 standard says something similar:
If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned to indicate an error, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.
Posted Jan 31, 2022 9:43 UTC (Mon)
by NYKevin (subscriber, #129325)
[Link]
The fact that it happens to vaguely resemble some feature of Lisp, but the standard does not actually require it to fulfill all the requirements of that feature is, frankly, Lisp's problem, not C's problem.
Posted Jan 31, 2022 10:02 UTC (Mon)
by larkey (guest, #104463)
[Link]
Anyway, the POSIX standard pretty clearly says that argv is NULL-terminated, that is, not by itself a zero-length array, but at least
char **argv = { NULL };
Handling argc==0 in the kernel
>It is legal to call malloc(0), and it is legal for malloc(0) to return a value other than NULL. If it does so, then the only thing you're allowed to do with that value is pass it to free()
malloc(0)
malloc(0)
malloc(0)
Handling argc==0 in the kernel