printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
Nov 14 '05
188 17428
Antoine Leca wrote:
.... snip ... I was pointing out that specifying a behaviour for fprintf("%p", (void*)0) would be useless if (because of the symmetry) you did not specify that fscanf("%p", &p) could make a null pointer to be stored in p. And I guess while a number of /implementations/ are in fact ready to digest the first form (as you pointed out), there is also a number of /programs/ that might *not* like receiving a null pointer.
From N869. I see nothing to prevent bandying NULL about.
p Matches an implementation-defined set of sequences,
which should be the same as the set of sequences
that may be produced by the %p conversion of the
fprintf function. The corresponding argument shall
be a pointer to a pointer to void. The input item
is converted to a pointer value in an
implementation-defined manner. If the input item is
a value converted earlier during the same program
execution, the pointer that results shall compare
equal to that value; otherwise the behavior of the
%p conversion is undefined.
Color me dense, but I see no real use for the facility.
--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
[fscanf which could create null pointers if the standard is revised, while
now it can't.]
En 42************* **@yahoo.com, CBFalconer va escriure: I see nothing to prevent bandying NULL about.
If the input item is a value converted earlier during the same program execution, the pointer that results shall compare equal to that value; otherwise the behavior of the %p conversion is undefined.
Since previous use of printf cannot convert a null pointer, you presently
fear no risk to actually receive an input item that could be converted into
a null pointer, since such a thing (that input item) is not reputed to exist
at all.
Now if the text for printf is updated, _automatically_ (no change of words)
fscanf can now store null pointers for the %p conversions. Which might upset
people which are writing e.g. portable libraries, because that now mean they
should check for these new null pointers, causing code bloat.
I see no real use for the facility.
Which one? The %p conversion? fscanf? ;-)
Antoine
aegis wrote: p The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.
(void*)0 is a valid value of the type "pointer to void".
Thus printf is required to accept it in this context.
pete wrote: How about printing the value of the one past pointer, do you think that is undefined? printf("&a + 1 is %p.\n", (void *)(&a + 1));
There's another case of a valid value for a pointer of
the type "pointer to void". Thus printf is required to
accept it and do the right thing.
Antoine Leca wrote: If the input item is a value converted earlier during the same program execution, the pointer that results shall compare equal to that value; otherwise the behavior of the %p conversion is undefined. Since previous use of printf cannot convert a null pointer, you presently fear no risk to actually receive an input item that could be converted into a null pointer, since such a thing (that input item) is not reputed to exist at all.
No, that's all wrong. Not only can fprintf convert a null
pointer value of type void* using the %p format specifier,
but there is also no problem in fscanf converting that
text sequence back to a null pointer value, which of course
will compare equal to the original value.
Now if the text for printf is updated, _automatically_ (no change of words) fscanf can now store null pointers for the %p conversions. Which might upset people which are writing e.g. portable libraries, because that now mean they should check for these new null pointers, causing code bloat.
? Of course pointers need to be used properly. This is a
non-problem.
"Antoine Leca" <ro**@localhost .invalid> writes: [fscanf which could create null pointers if the standard is revised, while now it can't.]
En 42************* **@yahoo.com, CBFalconer va escriure: I see nothing to prevent bandying NULL about.
If the input item is a value converted earlier during the same program execution, the pointer that results shall compare equal to that value; otherwise the behavior of the %p conversion is undefined.
Since previous use of printf cannot convert a null pointer, you presently fear no risk to actually receive an input item that could be converted into a null pointer, since such a thing (that input item) is not reputed to exist at all.
Are you assuming that printf("%p\n", (void*)0) invokes undefined
behavior? Since that's the point under dispute here, you might want
to make that assumption explicit.
--
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.
"Douglas A. Gwyn" <DA****@null.ne t> writes: aegis wrote: > p The argument shall be a pointer to void. The value of the > pointer is converted to a sequence of printing characters, in > an implementation-defined manner.
(void*)0 is a valid value of the type "pointer to void". Thus printf is required to accept it in this context.
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:
Each of the following statements applies unless explicitly stated
otherwise in the detailed descriptions that follow: If an argument
to a function has an invalid value (such as [...], or a null
pointer, [...]) or [...], the behavior is undefined.
This doesn't *quite* state that a null pointer is invalid unless
explicitly stated otherwise, but it's easy to interpret it that way.
The description of free() explicitly states that a null pointer is
allowed. The description of strlen() doesn't say anything about null
pointers, but we can infer from the above *and* from the description
of the argument ("the string pointed to by s") that a call invokes
undefined behavior if s doesn't point to a string.
On the other hand, the description of the 'p' specifier doesn't say
anything about null pointers. Since 7.1.4p1 weakly implies that null
pointers are invalid, it's too easy to infer that
printf("%p\n", (void*)0) invokes undefined behavior.
Common sense tells me that it *shouldn't* invoke undefined behavior,
but rather it should produce some implementation-defined textual
representation of a null pointer value, and I'm certain that that was
the intent. More specifically, I believe that any pointer value that
can be evaluated without invoking undefined behavior should not invoke
undefined behavior when passed to printf("%p", ...).
But I'm not comfortable depending on "common sense" to determine what
the standard means.
I can imagine a stubborn implementer creating a version of printf()
that depends on the pointer value being non-null (perhaps it shows it
as a base address and offset, or perhaps it attempts to show a few
bytes of what it points to). If I filed a bug report, I'm not sure
what chapter and verse I could cite to convince the implementer that
this is a bug. This probably isn't a realistic situation (I doubt
that any actual printf() implementation blows up on null pointers),
but it's still the kind of thing the standard should address.
--
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 Tue, 08 Mar 2005 20:18:22 GMT in comp.std.c, Keith Thompson
<ks***@mib.or g> wrote: "Douglas A. Gwyn" <DA****@null.ne t> writes:
aegis wrote:
> p The argument shall be a pointer to void. The value of the > pointer is converted to a sequence of printing characters, in > an implementation-defined manner.
(void*)0 is a valid value of the type "pointer to void". Thus printf is required to accept it in this context.
But part of the standard can be read to imply that (void*)0 is not a valid value of type "pointer to void".
But I'm not comfortable depending on "common sense" to determine what the standard means.
I can imagine a stubborn implementer creating a version of printf() that depends on the pointer value being non-null (perhaps it shows it as a base address and offset, or perhaps it attempts to show a few bytes of what it points to). If I filed a bug report, I'm not sure what chapter and verse I could cite to convince the implementer that this is a bug. This probably isn't a realistic situation (I doubt that any actual printf() implementation blows up on null pointers), but it's still the kind of thing the standard should address.
Isn't undefined behaviour any behaviour not explicitly defined in the
standard for the abstract machine or the library implementation?
So is the above (undefined) case not then undefined behaviour, as is
the case with strlen()?
The behaviour of printf() with format specifier "%p" is implementation
defined, but ISTM that leaves it open to each implementation whether
they define that part of the implementation to work will null
pointers.
If it could be undefined behaviour, we have no option but to treat it
as undefined behaviour in portable code.
--
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada Br**********@CS i.com (Brian[dot]Inglis{at}Syste maticSW[dot]ab[dot]ca)
fake address use address above to reply
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.
In article <ln************ @nuthaus.mib.or g>, Keith Thompson <ks***@mib.or g> writes: "Peter Nilsson" <ai***@acay.com .au> writes: [...] Consider a cpu with 24-bit address space, but 32-bit address/data registers. Such a machine may have 255 or more null pointer representations , none of which point to a genuine addressable memory location.
Actually it could have 4278190080 such pointer representations (2**32 - 2**24). (Whether they all be treated as C null pointers is another question.)
For a real-world example, let me invoke once again the mighty AS/400:
for its (conforming C90) implementations , the null pointer is
all-bits-zero, but all valid non-null pointer values have bit 127
set. Thus the '400 has 2**127 invalid pointer representations , but
only one is a null pointer.
(To approach the ostensible topic of this thread a bit, awhile back I
posted the output of %p formatting for some AS/400 pointers, I
believe. They're ... distinctive.)
--
Michael Wojcik mi************@ microfocus.com
She felt increasingly (vision or nightmare?) that, though people are
important, the relations between them are not, and that in particular
too much fuss has been made over marriage; centuries of carnal
embracement, yet man is no nearer to understanding man. -- E M Forster 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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: 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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| | |