←back to thread

48 points ingve | 1 comments | | HN request time: 0.363s | source
Show context
AaronDinesh ◴[] No.44389347[source]
Why should it be allowed to return a valid pointers anyways? Surely it should always return NULL?
replies(5): >>44389371 #>>44389461 #>>44389531 #>>44390081 #>>44392719 #
cjensen ◴[] No.44389531[source]
There are three reasonable choices: (a) return the null pointer (b) return a valid unique pointer and (c) abort().

The point of the original C Standard was to make rules about these things AND not break existing implementations. They recognized that (a) and (b) were in existing implementations and were reasonable, and they chose not to break the existing implementations when writing the standard.

This is similar to the extremely unfortunate definition of the NULL macro. There were two existing styles of implementation (bare literal 0 and (void *) 0) and the Standard allows either style. Which means the NULL macro is not entirely safe to use in portable code.

replies(1): >>44389592 #
commandlinefan ◴[] No.44389592[source]
> return a valid unique pointer

A pointer to what, though? If the requester asked for 0 bytes of memory, you'd either be pointing to memory allocated for another purpose (!) or allocating a few bytes that weren't asked for.

> This makes people unhappy for various reasons

I read through all the links trying to figure out what those reasons might be and came up empty, I'm still curious why anybody would expect or rely on anything except a null pointer in this instance.

replies(4): >>44389671 #>>44389719 #>>44389745 #>>44389786 #
1. AaronAPU ◴[] No.44389745[source]
The only requirement which seems reasonable to me, is that the address be unique. Since the allocation size is zero, it should never be accessed for read or write, but the address itself may need to be used for comparisons.

If you’re pointing to a zero sized data it shouldn’t matter what it’s pointing to. Even outside valid address space. Because you shouldn’t be reading or writing more than 0 bytes anyway.