Re: printf("%p\n", (void *)0);

From: Douglas A. Gwyn (DAGwyn_at_null.net)
Date: 03/09/05


Date: Wed, 9 Mar 2005 00:13:18 GMT

Keith Thompson wrote:
> But part of the standard can be read to imply that (void*)0 is not a
> valid value of type "pointer to void".
> 7.1.4p1 says:

That's a misreading. The parenthetical phrase "(such as ...)"
is intended to clarify what is meant by "an invalid value",
not to completely characterize the set of invalid values.
In many function-call contexts a null pointer argument would
indeed be invalid, e.g. when a pointer to an array is specified,
but not in the case of printf("%p",vp), where any well-defined
value is permitted. (A null pointer has a "valid" value.)

> On the other hand, the description of the 'p' specifier doesn't say
> anything about null pointers.

It doesn't have to. A null pointer value with type "pointer to
void" is just one case covered by the fprintf spec, and in this
context it does not have any special properties.



Relevant Pages

  • Re: null references
    ... To say that null and invalid references don't exist suggests that if you would just change to the function would be unable to fail and therefore correctly manages the semaphore, but this is a bogus argument. ... The reference version of the function can still throw an access violation and still needs to be coded for exception safety. ... If you define the above function as taking a reference arg then the undefined behaviour occurs the moment someone tries to dereference an invalid pointer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: access_ok macor
    ... The problem which I have is that if I run socketpair, getsockname, getpeername LTP ... tests with invalid salen pointer there are addresses close to 0x0. ... This sounds like a classic NULL pointer dereference that is handled correctly ... err = get_user; ...
    (Linux-Kernel)
  • Re: interpreting a null pointer as an empty (null string)
    ... there requires doing something that has undefined behavior (or, at least, ... which is the usual destination of the null pointer. ... Looks like this is explicitly disallowed by the Standard, ... null pointers being "invalid" as operands of the indirection operator; ...
    (comp.lang.c)
  • Re: null references
    ... invalid to cause an access violation, ... The reference version of the function can still throw an access ... obtained by dereferencing a null pointer which causes undefined behaviour". ... occurs before the function has even started so how can it protect itself. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Reading from invalid memory
    ... from the same invalid memory location I do not get any errors. ... since constructing or dereferencing an invalid pointer ... Undefined behavior can be anything, ...
    (comp.unix.programmer)