News for hackers. Stuff that matters.
Re:Stop Memsetting Structures by andrewshadura on Saturday April 27, 2019 @08:02PM (#19768472)
By the standard, the null pointer must be equivalent to 0.
Re:Stop Memsetting Structures by yuubi on Saturday April 27, 2019 @08:26PM (#19768548)
Only if you use conversion within the language: void *p=0;

If you said memset(&p, 0, sizeof(p)) then that's not guaranteed to work on all implementations.

Re:Stop Memsetting Structures by uryga on Saturday April 27, 2019 @08:42PM (#19768594)
gizmo686's comment [1] explains the subtle difference:

> [...] 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.

[1] https://news.ycombinator.com/item?id=19767316

Re:Stop Memsetting Structures by nybble41 on Monday April 29, 2019 @12:36AM (#19775862)
The memory representation of the null pointer is implementation-defined. The C99 standard only guarantees that a integer constant expression with the value 0, implicitly or explicitly cast to a pointer type, will give a null pointer. It does not guarantee that a null pointer cast to an integer type (other than _Bool) will give the value 0; all pointer-to-integer casts are implementation-defined. It also does not guarantee that a non-constant expression which evaluates to 0 will produce a null pointer if cast to a pointer type.

In short, there may exist counter-intuitive but nonetheless standards-compliant platforms where tests such as "assert((uintptr_t)(void * )0 == 0);" or "int x = 0; assert((void * )x == (void * )0);" would fail.