4 ms·
> it's possible for the assertion to fail, since NULL it not guaranteed to be represented in memory by a zero bytes. Technically correct (which is of course th
by ThrowawayR2 8y ago
> it's possible for the assertion to fail, since NULL it not guaranteed to be represented in memory by a zero bytes.
Technically correct (which is of course the best kind of correct) but you'd be hard pressed to find a system in 2019 where NULL != (void*)0. The most recent machines with non-zero NULL in the C FAQ entry on the matter (http://c-faq.com/null/machexamp.html http://c-faq.com/null/machexamp.html) date back to the mid '90s.
- cperciva 8y ago> you'd be hard pressed to find a system in 2019 where NULL != (void *)0 You'd be hard pressed to find a system at any time where NULL is not equal to a compile-time constant zero. ;-)
- hedora 8y ago> The Prime 50 series used segment 07777, offset 0 for the null pointer, BAM!!! (All I had to do was click on the link in the comment you replied to, but the pedantry in this subthread is glorious, and I had to contribute — downvoters, this is your chance to school me on HN etiquette!)
- cperciva 8y agoYou're missing the point. "(void *)0" is guaranteed to be a NULL pointer, even when said pointer is not represented in memory by zeroes.
- gizmo686 8y ago>NULL != (void* )0. This expression is false in any C99 compliant system: Section 6.3.2.3 [0]: > An integer constant expression with the value 0, or such an expression cast to type void * , is called a null pointer constant. [1] If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function > Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal. The technicality of the memset example is that it does not set the bits of the pointer by referring to it as a pointer, so the requirement that 0 behave as if it was a null pointer does not apply. [0] http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf [1] In text footnote: The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant; see 7.17 EDIT: Formatting
- Sharlin 8y agoThe literal `0` is guaranteed to be the null pointer value when used as a pointer, so by definition NULL == (void*)0. But given void *p = 0; intptr_t i = 0; it is not guaranteed that `memcmp(&i, &p, sizeof(p)) == 0`.
- nybble41 8y ago> it is not guaranteed that `memcmp(&i, &p, sizeof(p)) == 0`. Even less intuitively, it is not guaranteed that `(void*)i == p` since `i` is not an integer constant expression, even if the value is known to be 0.
- loeg 8y agoNah, NULL is guaranteed to be == (void * )0 by the standard. The allowed divergence is in how (void * )0 is represented as bits in memory, i.e., memcmp(zeroes, (ptr = NULL), sizeof(ptr)) == 0?
- rightbyte 8y agoTechnically no such system exists ;) But talking about the actual bit representation. A zero value as a special pointer with the magic property of being invalid is a convention for C on x86, right? I assume it has to do with it's fast to check the zero-flag in EFLAGS register when doing checks for null pointers.