printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
Nov 14 '05
188 17422
Quentarez <qu*******@cogn itiveprocess.co m> writes: On Sat, 5 Mar 2005 22:20:00 +0000 (UTC), infobahn wrote:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */ I, personally, would not consider this to be UB.
I don't have supreme knowledge of the standard, nor do I have any references to the standard to support this claim.
I decided to take a different route, and see what the compiler does with a NULL pointer. I tested it on two compilers: MSVC6 and gcc 3.3.5.
Observing the behavior of any number of implementations doesn't
demonstrate that the behavior is defined. Undefined behavior is
exactly that, undefined. Specifically, it's
behavior, upon use of a nonportable or erroneous program construct
or of erroneous data, for which this International Standard
imposes no requirements
(C99 3.4.3p1)
Even assuming that it's UB, the behaviors of MSVC6 and gcc are
entirely consistent with the standard. (*Any* behavior would be
entirely consistent with the standard.)
The only way to determine whether a given construct invokes undefined
behavior is by a careful reading of the standard.
It has already been shown previous to this post that (void *)0 is indeed a way to get a NULL pointer, so I will refer to it as a NULL pointer from now on.
It's better to refer to this as a "null pointer", not a "NULL
pointer". NULL is a macro that expands to a null pointer constant;
using all-caps NULL for anything else is misleading.
From what I've learned, a pointer always points /somewhere/, even if that somewhere is not where you intended.
Not necessarily. As far as the language is concerned a null pointer
points nowhere. (It might happen to point to some location in
physical or virtual memory, but no program can determine that without
invoking undefined behavior.)
More concretely, on a system with 128 megabytes of virtual memory, a
pointer with the value 0x10000000 (that's 256 megabytes) doesn't point
anywhere -- or maybe it points to a memory card you haven't installed
yet. (For the sake of this example, I'm making several assumptions
about pointers that are commonly true, but are not required by the
standard.)
From simply compiling and running the given code, I get the output 00000000 on MSVC6 and (nil) on gcc.
Using gcc on three different platforms, I get "0x0", "0", and "(nil)".
It's the runtime library, not the compiler, that implements printf.
(As far as the standard is concerned, the compiler and the library are
both part of the implementation; in other words, "gcc" is not a
complete description of the implementation. )
[snip]
Based on my observations, it seems that
printf("%p\n", (void *)0);
is not UB.
Based on your observations, it could very well be UB.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
CBFalconer <cb********@yah oo.com> writes: Andrey Tarasevich wrote: ... snip ... This has already been covered above. Once again, the standard says that null pointer arguments can be specifically allowed by the specification of the concrete function from the standard library. 'free' function is a good example of such function: its specification explicitly states that passing a null pointer to it is OK (does noting). The specification of 'printf' does not say that passing null pointers to it is OK. That's exactly what constitutes the problem.
However the standard does say that dereferencing NULL is bad, and that is exactly what is required for all usages of NULL in printf except the %p descriptor. It is that omission that is giving some people qualms.
For that matter, a printf implementation *could* attempt to
dereference the pointer associated with the "%p" descriptor (after an
appropriate conversion, of course). It might be useful for the "%p"
output to show both the value of the pointer and, say, the first few
bytes of the memory it points to. "The value of the pointer is
converted to a sequence of printing characters, in an
implementation-defined manner."
In my opinion such an implementation that blew up attempting to
dereference a null pointer would be less than useful, and would
violate what I presume to be the intent of the standard, but the
actual wording of the standard doesn't quite forbid it.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
On Sun, 06 Mar 2005 20:48:01 GMT, Keith Thompson wrote: Quentarez <qu*******@cogn itiveprocess.co m> writes: On Sat, 5 Mar 2005 22:20:00 +0000 (UTC), infobahn wrote:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
I, personally, would not consider this to be UB.
I don't have supreme knowledge of the standard, nor do I have any references to the standard to support this claim.
I decided to take a different route, and see what the compiler does with a NULL pointer. I tested it on two compilers: MSVC6 and gcc 3.3.5.
Observing the behavior of any number of implementations doesn't demonstrate that the behavior is defined. Undefined behavior is exactly that, undefined. Specifically, it's
behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements
(C99 3.4.3p1)
Even assuming that it's UB, the behaviors of MSVC6 and gcc are entirely consistent with the standard. (*Any* behavior would be entirely consistent with the standard.)
The only way to determine whether a given construct invokes undefined behavior is by a careful reading of the standard.
It has already been shown previous to this post that (void *)0 is indeed a way to get a NULL pointer, so I will refer to it as a NULL pointer from now on.
It's better to refer to this as a "null pointer", not a "NULL pointer". NULL is a macro that expands to a null pointer constant; using all-caps NULL for anything else is misleading.
From what I've learned, a pointer always points /somewhere/, even if that somewhere is not where you intended.
Not necessarily. As far as the language is concerned a null pointer points nowhere. (It might happen to point to some location in physical or virtual memory, but no program can determine that without invoking undefined behavior.)
More concretely, on a system with 128 megabytes of virtual memory, a pointer with the value 0x10000000 (that's 256 megabytes) doesn't point anywhere -- or maybe it points to a memory card you haven't installed yet. (For the sake of this example, I'm making several assumptions about pointers that are commonly true, but are not required by the standard.)
From simply compiling and running the given code, I get the output 00000000 on MSVC6 and (nil) on gcc.
Using gcc on three different platforms, I get "0x0", "0", and "(nil)". It's the runtime library, not the compiler, that implements printf. (As far as the standard is concerned, the compiler and the library are both part of the implementation; in other words, "gcc" is not a complete description of the implementation. )
[snip]
Based on my observations, it seems that
printf("%p\n", (void *)0);
is not UB.
Based on your observations, it could very well be UB.
Ah! My definition of UB was completely incorrect. Thank you very much for
your response and clarification! :)
Keith Thompson wrote: Quentarez <qu*******@cogn itiveprocess.co m> writes: On Sat, 5 Mar 2005 22:20:00 +0000 (UTC), infobahn wrote:
printf("%p\n", (void *)0); /* UB, or not? Please explain your
answer. */ I, personally, would not consider this to be UB.
I don't have supreme knowledge of the standard, nor do I have any references to the standard to support this claim.
I decided to take a different route, and see what the compiler does
with a NULL pointer. I tested it on two compilers: MSVC6 and gcc 3.3.5. Observing the behavior of any number of implementations doesn't demonstrate that the behavior is defined. Undefined behavior is exactly that, undefined. Specifically, it's
behavior, upon use of a nonportable or erroneous program
construct or of erroneous data, for which this International Standard imposes no requirements
(C99 3.4.3p1)
Even assuming that it's UB, the behaviors of MSVC6 and gcc are entirely consistent with the standard. (*Any* behavior would be entirely consistent with the standard.)
The only way to determine whether a given construct invokes undefined behavior is by a careful reading of the standard.
It has already been shown previous to this post that (void *)0 is
indeed a way to get a NULL pointer, so I will refer to it as a NULL pointer
from now on.
It's better to refer to this as a "null pointer", not a "NULL pointer". NULL is a macro that expands to a null pointer constant; using all-caps NULL for anything else is misleading.
From what I've learned, a pointer always points /somewhere/, even
if that somewhere is not where you intended.
Not necessarily. As far as the language is concerned a null pointer points nowhere. (It might happen to point to some location in physical or virtual memory, but no program can determine that without invoking undefined behavior.)
It does point somewhere. It points to no object. And 'no object' does
not
mean 'nowhere'.
--
aegis
On 2005-03-06 20:56, Keith Thompson wrote:
: For that matter, a printf implementation *could* attempt to dereference the pointer associated with the "%p" descriptor (after an appropriate conversion, of course). It might be useful for the "%p" output to show both the value of the pointer and, say, the first few bytes of the memory it points to. "The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner."
The first few bytes of memory it points to is not part of the value
of the pointer, though (well, unless it points to itself...).
Arguably, converting "the value of the pointer" should never require
dereferencing the value.
-- Niklas Matthies
Niklas Matthies <us***********@ nmhq.net> writes: On 2005-03-06 20:56, Keith Thompson wrote: : For that matter, a printf implementation *could* attempt to dereference the pointer associated with the "%p" descriptor (after an appropriate conversion, of course). It might be useful for the "%p" output to show both the value of the pointer and, say, the first few bytes of the memory it points to. "The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner."
The first few bytes of memory it points to is not part of the value of the pointer, though (well, unless it points to itself...). Arguably, converting "the value of the pointer" should never require dereferencing the value.
That's a good point. My thought is that the phrase "in an
implementation-defined manner" might leave enough leeway to look at
what it points to (and it could actually be useful). But since it's a
void*, it's hard to decide how many bytes to display.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote: Niklas Matthies <us***********@ nmhq.net> writes: On 2005-03-06 20:56, Keith Thompson wrote: For that matter, a printf implementation *could* attempt to dereference the pointer associated with the "%p" descriptor (after an appropriate conversion, of course).
And printing an integer could cause that integer to be converted
to a pointer and subsequently dereferenced. The standard doesn't
seem to preclude it, so it must be an option, right? ;)
It might be useful for the "%p" output to show both the value of the pointer and, say, the first few bytes of the memory it points to.
Your use of the word 'both' implies you already recognise the
distinction.
"The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner."
The first few bytes of memory it points to is not part of the value of the pointer, though (well, unless it points to itself...). Arguably, converting "the value of the pointer" should never require dereferencing the value.
That's a good point. My thought is that the phrase "in an implementation-defined manner" might leave enough leeway to look at what it points to...
Think about these...
char a[42];
printf("%p\n", a + 42);
char *p = malloc(0);
printf("%p\n", p);
More pragmatically, consider a conforming implementation
that allows pointers to be generated for all sorts of things,
including memory mapped I/O. Such an implementation would have
to check the pointer _very carefully_ to determine that
subsequent dereferencing will not reformat the harddisk!
--
Peter
"aegis" <ae***@mad.scie ntist.com> writes: Keith Thompson wrote: Quentarez <qu*******@cogn itiveprocess.co m> writes:
[...] > From what I've learned, a pointer always points /somewhere/, even > if that somewhere is not where you intended.
Not necessarily. As far as the language is concerned a null pointer points nowhere. (It might happen to point to some location in physical or virtual memory, but no program can determine that without invoking undefined behavior.)
It does point somewhere. It points to no object. And 'no object' does not mean 'nowhere'.
Yes, it does. There isn't some mythical object called "no object".
"It points to no object" means the same as "It doesn't point to an
object".
To use a physical analogy, suppose I have a laser pointer that I can
use to point to various objects. If I aim it at an empty spot in the
sky, it's still pointing, but it's not pointing to any object. If I
turn it off, it's not pointing.
A null pointer is like a laser pointer that's turned off.
If you view it as a raw machine-level address (physical or virtual,
whichever is used for C pointers), a null pointer *might* happen to be
the address of some actual chunk of memory, or it might contain a
virtual address that's not mapped into the current process's address
space or a physical address that doesn't correspond to any real
memory. The C standard doesn't distinguish between these cases; as
far as C is concerned, a null pointer doesn't point. No program can
determine whether a null pointer acts as if it pointed to something
without invoking undefined behavior.
What the C standard says is that it "is guaranteed to compare unequal
to a pointer to any object or function"; it also says that applying
the unary "*" operator to an invalid pointer (including a null
pointer) invokes undefined behavior.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson <ks***@mib.or g> writes: For that matter, a printf implementation *could* attempt to dereference the pointer associated with the "%p" descriptor (after an appropriate conversion, of course). It might be useful for the "%p" output to show both the value of the pointer and, say, the first few bytes of the memory it points to.
That would make the following impossible to implement portably
without invoking undefined behavior, whereas I don't think it
should be:
printf("%p\n", malloc(0));
A real implementation would probably have extra knowledge though.
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin
Keith Thompson wrote: "aegis" <ae***@mad.scie ntist.com> writes: Keith Thompson wrote: Quentarez <qu*******@cogn itiveprocess.co m> writes: [...] > From what I've learned, a pointer always points /somewhere/,
even > if that somewhere is not where you intended.
Not necessarily. As far as the language is concerned a null
pointer points nowhere. (It might happen to point to some location in physical or virtual memory, but no program can determine that
without invoking undefined behavior.)
It does point somewhere. It points to no object. And 'no object' does not mean 'nowhere'.
Yes, it does. There isn't some mythical object called "no object". "It points to no object" means the same as "It doesn't point to an object".
Noone said it is mythical. The standard clearly states that
the purpose of a null pointer is to distinguish between
pointing to an object and no object. You can think of it as having
a set of objects with one object for representing no object.
Let's call the special object the sentinel.
When our pointer points to the sentinel, it means 'no object'.
When our pointer is not pointing to the sentinel, it is pointing
to an object(except where the value is invalid). In either case,
our pointer points to something but is never 'turned off' since
it is always functioning. That is the point of pointers, you see?
To point to things. If the pointer were to be 'turned off' it would
stop functioning, in which case we would be unable to determine
if it points to the object that represents no object. ;-)
--
aegis This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kwan Ting |
last post by:
The_Sage, I see you've gotten yourself a twin asking for program in
comp.lang.c++ .
http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1
If you the oh so mighty programmer that you pretend to be, why don't you
just write some? (And oh, void main is still not allow by the C++ standard.)
Seeming as how you tried to quote the standard in an attempt to pretend
you're right, I'll quote the standard to once...
|
by: dam_fool_2003 |
last post by:
Hai,
I thank those who helped me to create a single linked list with int
type. Now I wanted to try out for a void* type. Below is the code:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<stddef.h>
struct node
|
by: Stig Brautaset |
last post by:
Hi group,
I'm playing with a little generic linked list/stack library, and have a
little problem with the interface of the pop() function. If I used a
struct like this it would be simple:
struct node {
struct node *next;
void *data;
};
|
by: Jason luo |
last post by:
Hi all,
In c99-standard page 52,there is a sentence about void,as below:
If an expression of any other type is evaluated as a void expression,
its value or designator is discarded.
I don't know how to understand it, How to evaluate the expression as a
void expression? explicit conversion the expression? but, befor the
sentence ,"implicit or explicit conversions (except to void) shall not
|
by: sunglo |
last post by:
My doubt comes from trying to understand how thread return values work
(I know, it's off topic here), and I'm wondering about the meaning of
the "void **" parameter that pthread_join expects (I think this is
topical, since it's a C question, but please correct me and apologies
if I'm wrong).
I suppose that it's this way to allow for "generic" pointer
modification, but this implies dereferencing the "void **" pointer to
get a "void *"...
| |
by: Juggernaut |
last post by:
I am trying to create a p_thread
pthread_create(&threads, &attr, Teste, (void *)var);
where var is a char variable.
But this doesnt't work, I get this message:
test.c:58: warning: cast to pointer from integer of different size.
Now I thought that when it was a void I could pass anything? Thing is it
works when I use an int, but in this case I wanted to use a char. It wouldnt
be hard to work around it, but it annoys me because I've heard...
|
by: Stijn van Dongen |
last post by:
A question about void*. I have a hash library where the hash create
function accepts functions
unsigned (*hash)(const void *a)
int (*cmp) (const void *a, const void *b)
The insert function accepts a void* key argument, and uses the
functions above to store this argument. It returns something (linked to
the key) that the caller can store a value in. The actual key argument
is always of the same pointer type (as seen in the caller,...
|
by: maadhuu |
last post by:
hello, this is a piece of code ,which is giving an error.
#include<stdio.h>
int main()
{
int a =10;
void *p = &a;
printf("%d ", *p ); //error....why should it //be an error ?can't the
compiler make out because //of %d that the pointer is supposed to refer to
an integer ?? or is explicit type casting required ??
|
by: Erik de Castro Lopo |
last post by:
Hi all,
The GNU C compiler allows a void pointer to be incremented and
the behaviour is equivalent to incrementing a char pointer.
Is this legal C99 or is this a GNU C extention?
Thanks in advance.
Erik
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |