4 ms·
It's a pet peeve of mine that C people seem to think that 0 isn't a number. Or that 0 (or -1) mean infinity. (Like a timeout of 0 meaning an infinite timeout,
by eru 3y ago
It's a pet peeve of mine that C people seem to think that 0 isn't a number. Or that 0 (or -1) mean infinity. (Like a timeout of 0 meaning an infinite timeout, instead of failing immediately.)
I guess it's mostly a consequence of C having such poor support for data types.
For a low-level allocator interface this might be an acceptable compromise, but C people also tend to use this kind of mangling when it's not warranted.
- saagarjha 3y agoAn allocation of size zero cannot be used for anything useful.
- lifthrasiir 3y agoThe caller may still want to distingish a successful yet useless allocation from an unsuccessful allocation, which resulted in the current situation.
- saagarjha 3y agoHow would you tell?
- Nevermark 3y agoZero bytes are always allocatable. Unless you need zero byte (and other) pointers to be consistently unique. But that would mean allocating and deallocating some memory for zero byte requests, to provide unique addresses. Or some reserved memory address span, outside of available (real and virtual) RAM, for zero byte pointers.
- lifthrasiir 3y ago> Unless you need zero byte (and other) pointers to be consistently unique. Yes, this was the problem. People had some different ideas for contracts that `malloc` etc. should guarantee---the most lax requirement would be a "usable" pointer on success, which does almost perfectly constrain non-zero cases but leaves lots of freedom for zero cases. At the opposite side there is a "unique non-NULL" pointer requirement, as you've said, which essentially makes `malloc(0)` almost same to `malloc(1)`. Rust also has this issue because its reference, a pointer after compilation, is expected to be non-null even for zero-sized types. Rust's rule is somewhat in between: any pointer should be non-null, but it doesn't have to be unique for zero-sized types. The canonical non-null pointer for such types (as returned by `NonNull::dangling`) is the smallest non-zero pointer that satisfies the alignment.
- dwattttt 3y agoYou could definitely use it for something useful; you can't store anything through it, but if an allocator supports allocations of size 0 with the same semantics of non-zero allocations, you get a "unique" ptr from the allocator dynamically. Anywhere you need a dynamic list identifier this would suit (yes, a counter could do this too, this would be effectively a globally unique token)
- optimiz3 3y agoOne example is a non-null sentinel/oracle allocation. Often you see this in lockless data structures where you don't want to do a null check.
- eru 3y agoIt should still work. Just like adding 0 to a number should still work, or multiplying by 1. The computer should just happily do nothing.
- astrobe_ 3y ago> It's a pet peeve of mine that C people seem to think that 0 isn't a number. Or that 0 (or -1) mean infinity. It's not just C people :D - Zero may or may not be a natural number depending on who you ask, and negative numbers definitely are not. And since in low-level programming, which is kind of C's kingdom, every bit might count and you'd rather use that useless sign bit for something else. -1 being particularly liked by compilers because it is "all bits set" in the dominant two's complement sign encoding, which sometimes allows optimization tricks. > I guess it's mostly a consequence of C having such poor support for data types. Technically nothing prevents anyone from returning a small two fields structure - one for a tag and one for the value - to have "tagged unions" or "optional types" almost like your favorite language. But in the eyes of a low-level C programmer, that's one field too much (omg that doesn't fit in a register, you will be held responsible for global warming if you publish this etc.). So, it's not that we think that anything non-positive is not a "true number", it's more like we've been using these conventions sometimes since assembly programming days (OR AX,AX to test if register AX is zero in old x86 assembly, because the OR instruction sets the CPU's Z flag) in order to get maximum performance.
- eru 3y agoYou are right about cramming things into bits. But it often makes code more complicated. Eg if you have two operations you do one after another and they have time-outs of t1 and t2, then normally, you know that after t1+t2, either both will have succeeded, or they will have failed. But that breaks down with the 'C-conventions' when either t1 or t2 equals 0. Thus you need extra logic to deal with these. As an aside: OCaml uses tagged pointers to cram more information into machine words. That's an implementation technique, and (mostly) hidden from users of the language. (Apart from integers having 63 bits, instead of 64 bits size by default.) So that's an example where the language is 'clean', but the implementation is still efficient and uses all the low level tricks. And my pet peeve is more that many people always do this trickery, even when not every bit counts. > It's not just C people :D - Zero may or may not be a natural number depending on who you ask, and negative numbers definitely are not. Yes, but I never mentioned any restriction to Natural numbers. Btw, in the context of computers 0 is definitely a number that comes natural, even if it's not a Natural number by some people's definition.
- eska 3y agoThe number 0 is interesting in the sense that it is actually a comparatively late and abstract invention. The natural numbers are also mathematically defined as either including or excluding 0 depending on the author. That said the issue is really in-band signaling. “I already have this data type which I don’t fully use, let’s use one of the unused values to represent something completely unrelated..”
- eru 3y agoYes, in-band signalling is error-prone. If you want to do it properly, you better have good language support for it. (Eg dependent types might help?)