473,785 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
Nov 14 '05
188 17437
infobahn wrote:
But Keith Thompson has already agreed that, in cases where a
null pointer value has *explicit* semantics (fflush, strtok,
etc), it's clearly valid and there's no problem.


That counter-argument relies on "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 ..., the behavior is
undefined." Certainly if you examine the fprintf spec,
a null pointer value is not inherently invalid in that
context; that pointer argument is not required to point
at an object (unlike e.g. the arguments for strcpy).
You don't even need to refer to the parenthetical clause
("..." above), which gave examples of classes of values
that might be considered invalid, generally depending on
context. Not every use of a null pointer is invalid.
Nov 14 '05 #131
Stephen Sprunk wrote:
free(a);
printf("%p\n", a);


Don't do that. Thanks.
Nov 14 '05 #132
Keith Thompson wrote:
I've been wondering why the standard says that the value of a free()d
pointer becomes indeterminate and not that it becomes a trap
representation; ...


"Trap representation" was a C99 invention (mainly by Clive
Feather, during revision of the specs for integer reps),
and was meant to cover bit patterns that were not valid
values. Since in the case of a freed pointer it is not
the bit pattern but rather its *meaning* that may have
become indeterminate, "trap rep" didn't seem appropriate.
Nov 14 '05 #133
"Trevor L. Jackson, III" wrote:
I.e., if there were no intervening heap oprtations the most recently
free()'d block was still available *and it's contents unchanged*.
Clearly that is no longer true.


That hasn't been true since C was standardized, and wasn't a
property to rely on even before that.
Nov 14 '05 #134
"Trevor L. Jackson, III" wrote:
So realloc() is explicitly defined to not handle stale pointers?


How would you pass the pointer value as an argument without
evaluating it?
Nov 14 '05 #135
pete <pf*****@mindsp ring.com> writes:
Richard Bos wrote:

dj******@csclub .uwaterloo.ca (Dave Vandervies) wrote:

> I do disagree with the claim that similar reasoning doesn't apply to
> fprintf's %p format specifier, which was the context of my statement
> above.


The definition of assert() explicitly mentions 0 values.
The definition
of printf("%p...) does not, and is AFAICT unique in this.


In all of the rest of the standard functions
where NULL is an invalid argument, the standard's description
of the function will define arguments in such a way
so as to exclude NULL, unlike the way printf is described.

When the standard says "the object pointed to by s1" or
"the initial element of which is pointed to by base"
then those words exclude NULL as a valid value for both s1 and base.

All of the library descriptions are like that.


If the behavior of printf("%p\n", (void*)0) depends on the way all the
other standard functions are described, I think it would be better to
describe the behavior of the "%p" format specifier.

I think I'd even be happy with a footnote.

--
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.
Nov 14 '05 #136
James Kuyper wrote:
Keith Thompson wrote:
pete <pf*****@mindsp ring.com> writes:
.... That's not how I understood Keith's argument. He's arguing that a single

pete's

Sorry! I was paying more attention to arguments than to attributions.
Nov 14 '05 #137
Keith Thompson wrote:
pete <pf*****@mindsp ring.com> writes:

....
When the standard says "the object pointed to by s1" or
"the initial element of which is pointed to by base"
then those words exclude NULL as a valid value for both s1 and base.

All of the library descriptions are like that.

If the behavior of printf("%p\n", (void*)0) depends on the way all the
other standard functions are described, I think it would be better to
describe the behavior of the "%p" format specifier.


That's not how I understood Keith's argument. He's arguing that a single
consistent approach is used: a null pointer argument is invalid whenever
the corresponding description of the behavior implies that the argument
is dereferenced. He's referring to the other descriptions only as
confirmation of this convention, not as something that must be read in
order to understand the description of printf().
Nov 14 '05 #138
James Kuyper wrote:

Keith Thompson wrote:
pete <pf*****@mindsp ring.com> writes: ...
When the standard says "the object pointed to by s1" or
"the initial element of which is pointed to by base"
then those words exclude NULL as a valid value for both s1 and base.

All of the library descriptions are like that.

a single consistent approach is used:
a null pointer argument is invalid whenever
the corresponding description of the behavior
implies that the argument is dereferenced.


In cases when it boils down to that the totality of the issue
is just that it confuses a couple of guys on c.l.c,
then the committee doesn't do anything.

--
pete
Nov 14 '05 #139
pete wrote:

James Kuyper wrote:

Keith Thompson wrote:
pete <pf*****@mindsp ring.com> writes:

...
>When the standard says "the object pointed to by s1" or
>"the initial element of which is pointed to by base"
>then those words exclude
> NULL as a valid value for both s1 and base.
>
>All of the library descriptions are like that.

a single consistent approach is used:
a null pointer argument is invalid whenever
the corresponding description of the behavior
implies that the argument is dereferenced.


Actually dereferencing isn't required to disqualify NULL
as a valid argument value.
In C99, a call to qsort is valid when nmemb equals zero.
While qsort is neither allowed to call the comparison function
nor perform any rearrangement when nemb is zero,it may still
do things like calculate the address of the last element
or the one after that, unconditionally ,
and (NULL + 0) and (NULL + 1) are undefined.
And so, NULL is an undefined value for base in a call to qsort,
even in C99, and even when nmemb equals zero.

void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
unsigned char *array, *after, *i, *j, *k, *p1, *p2, *end, swap;
size_t bytes;

array = base;
after = nmemb * size + array;
if (nmemb > (size_t)-1 / 3 - 1) {
nmemb = nmemb / 3 - 1;
} else {
nmemb = (nmemb * 3 + 1) / 7;
}
while (nmemb != 0) {
bytes = nmemb * size;
i = bytes + array;
do {
j = i - bytes;
if (compar(j, i) > 0) {
k = i;
do {
p1 = j;
p2 = k;
end = p2 + size;
do {
swap = *p1;
*p1++ = *p2;
*p2++ = swap;
} while (p2 != end);
if (bytes + array > j) {
break;
}
k = j;
j -= bytes;
} while (compar(j, k) > 0);
}
i += size;
} while (i != after);
nmemb = (nmemb * 3 + 1) / 7;
}
}

--
pete
Nov 14 '05 #140

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

192
9009
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...
6
9145
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
15
4167
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; };
3
5694
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
7
2489
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 *"...
9
5298
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...
5
3526
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,...
56
3365
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 ??
27
8978
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
0
9643
marktang
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...
0
9480
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,...
0
10147
jinu1996
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...
1
10087
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,...
0
9947
tracyyun
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...
0
8971
agi2029
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...
1
7496
isladogs
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...
0
6737
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();...
3
2877
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.