3 ms·
pointless, but not wrong: calloc on a zero width image should still yield a memory pointer, even if it points to a zero length stretch of memory. It would be st
by TheRealPomax 9y ago
pointless, but not wrong: calloc on a zero width image should still yield a memory pointer, even if it points to a zero length stretch of memory. It would be strictly wrong to return a null pointer just because (one of) the dimensions happens to be zero.
It might read a little funny, but it's actually quite correct with respects to what the code path should be expected to do. Valid dimensions should yield a valid pointer. Even if they're validly zero =)
- 8xde0wcNwpslOw 9y ago>It would be strictly wrong to return a null pointer just because (one of) the dimensions happens to be zero. Strictly wrong according to whom? At least not any C standard I know of. Besides, even if the function did return a non-null pointer value (which it is allowed to do; the behaviour is implementation-defined) you aren't allowed to dereference it any more than a null pointer, making it hardly any more "valid", either.
- TheRealPomax 9y agoAccording to code design mostly. It's not a syntax issue (both are fine syntax) but a logic issue. You should be able to trust that as long as your dimensions are valid, any for loop over the resulting list works. And in graphics contexts, zero is typically a perfectly valid value to set one (or both) dimensions to (negative values, however, are not). Of course, you _can_ say "I think 0 is an invalid dimension" but given that plenty of frameworks and applications consider 0 just fine, that's kind of an unnecessary restriction for the sake of writing a conditional that is arguably irrelevantly "easier" to read.
- meuk 9y agoYou could argue that a null pointer would be a correct pointer, since you are not allowed to dereference it anyway (since there is no data to access). It will probably break some assertions and assumptions though. Edit: I see that 8xde0wcNwpslOw (what a username) made the same point already.