3 ms·
Yes. I believe zero sized types should be possible and they should all have the same address. Trying to deref the pointer is UB right away because you do not ha
by Lvl999Noob 1y ago
Yes. I believe zero sized types should be possible and they should all have the same address. Trying to deref the pointer is UB right away because you do not have the byte under that pointer. As it is, the malloc implementation now needs special casing for 0 sized allocations and different implementations special case it differently. C is supposed to be low level so surface this confusion up. Let the programmer decide if they want a unique address and reserve a byte or a non unique one with no overhead.
- zzo38computer 1y agoZero sized types (and zero-length arrays, empty structures, etc) can be useful, and should not need to all have the same address (although in some cases they will have the same address, and in other cases it won't matter what the address is as long as it is nonzero), although you probably should not need to use dynamic allocation directly with such a type. GNU C has a Elvis operator so that you can write "malloc(x?:1)" if you really need to do that. If only a unique reference is needed (if it won't be used where other kind of pointers would also be used), and keeping track of allocation is wanted, one thing that may be possible is to just use a number, and then use a bit array to keep track of which numbers are in use.