3 ms·
> Even while writing this I realized I’d used malloc instead of calloc to allocate the entries array, which meant the keys may not have been initialized to NULL
by varajelle 6y ago
> Even while writing this I realized I’d used malloc instead of calloc to allocate the entries array, which meant the keys may not have been initialized to NULL.
I think this is still UB to allocate a struct containing pointer with calloc without explicitly set these pointers to 0 (or NULL).
The C standard doesn't specify the binary representation of the null pointer value.
In practice, that will work in virtually all current platforms, but it's still UB.
- ncmncm 6y agoAny particular platform can declare it to be defined, by fiat. "With our compiler, all zeroed raw storage treated as pointers are null." Anywhere that they technically could, you can treat it as if they actually had so declared, and just didn't publicize it enough.
- cygx 6y agoImplementation-defined as far as I'm aware. If pointers with all bits zero actually represent null pointers, things will be fine. edit: I haven't found any smoking gun quotation in the C11 standard on short notice, but there is footnote 296 (p.348) on calloc() zeroing all bits: > Note that this need not be the same as the representation of floating-point zero or a null pointer constant. Nothing about this being UB in cases where it does represent a null pointer constant.
- benhoyt 6y agoWow, thanks, I did not know that. Though that would be a kinda crazy platform or implementation! I'm betting that a lot of people depend on this.
- dpedu 6y agoNULL is guaranteed to equal integer 0 by standard, and converting 0 to a pointer is guaranteed to be a NULL pointer. > An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. 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.
- layer8 6y agoAn integer constant expression is a syntactical construct, not a runtime entity. The C standard provides no guarantee that bytes (chars) with value zero can be read from memory as a null pointer.
- adamrezich 6y agowait what? my C is a bit rusty but when would reading 0 from memory & casting to a (void?) pointer type ever != NULL?
- layer8 6y agoC implementations are allowed to use a not-all-bits-zero representation for null pointer values. They are merely required to convert integer constant expressions with value zero to a null pointer value — regardless of how the resulting null pointer value is represented in memory. See also these FAQs: http://c-faq.com/null/machnon0.html http://c-faq.com/null/machnon0.html http://c-faq.com/null/machexamp.html http://c-faq.com/null/machexamp.html
- ajross 6y agoI think that's true only in the narrowest sense of the C language standard. At the ABI and runtime linkage level, platforms (yes, every single one of them) document data in the .bss section to be zero, and C compilers place pointers there that are defined to be initialized to NULL per the standard. So, no, in practice it's well-defined, fully specified behavior on any given platform.