473,216 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

Violating sequence point?


Here's a sample function which converts a string to all uppercase:

#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}
Would the "Sequence point rule" be violated if the code were changed to the
following:
#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p++ = toupper( *p ) );
}
--

Frederick Gotham
Jul 5 '06 #1
15 1617
On 2006-07-05, Frederick Gotham <fg*******@SPAM.comwrote:
>
Here's a sample function which converts a string to all uppercase:

#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}
Why is that assert() there? I can see no use for it. Why is the do there?
There's no use for any of that line.
>
Would the "Sequence point rule" be violated if the code were changed to the
following:
#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p++ = toupper( *p ) );
}
I think that's okay. It's still best to be clear:
while (*p = toupper ((unsigned char) *p))
*p++;

While that's terrible style IMO, there's no ambiguity.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 5 '06 #2
On 2006-07-05, Andrew Poelstra <ap*******@wpsoftware.netwrote:
On 2006-07-05, Frederick Gotham <fg*******@SPAM.comwrote:
>>
Here's a sample function which converts a string to all uppercase:

#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}
Why is that assert() there? I can see no use for it. Why is the do there?
There's no use for any of that line.
Never mind; I read your other post explaining your logic. You should
probably put a comment in there.

(For others' benefit, the test is because casting to unsigned char is more
of a bandaid than a solution.)

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 5 '06 #3
Frederick Gotham <fg*******@SPAM.comwrites:
Would the "Sequence point rule" be violated if the code were changed to the
following:
[...]
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p++ = toupper( *p ) );
}
Yes. There is no sequence point intervening between the
modification of p (in *p++) and its use (in *p). Although a
function call contains a sequence point, the compiler may elect
to evaluate the left side of the assignment and the function
argument before invoking the function call.

Furthermore, toupper may be (and often is) implemented as a macro
that does not contain the same sequence point that the equivalent
function does.
--
"The fact that there is a holy war doesn't mean that one of the sides
doesn't suck - usually both do..."
--Alexander Viro
Jul 5 '06 #4
Frederick Gotham wrote:
Here's a sample function which converts a string to all uppercase:

#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}
Would the "Sequence point rule" be violated if the code were changed to the
following:
#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p++ = toupper( *p ) );
}
Yes, as would be:

while (*p = toupper(*p++) );

Robert Gamble

Jul 5 '06 #5

Andrew Poelstra wrote:
On 2006-07-05, Andrew Poelstra <ap*******@wpsoftware.netwrote:
On 2006-07-05, Frederick Gotham <fg*******@SPAM.comwrote:
>
Here's a sample function which converts a string to all uppercase:

#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}
Why is that assert() there? I can see no use for it. Why is the do there?
There's no use for any of that line.

Never mind; I read your other post explaining your logic. You should
probably put a comment in there.
Which post would that be ?

Jul 5 '06 #6
Andrew Poelstra said:
On 2006-07-05, Andrew Poelstra <ap*******@wpsoftware.netwrote:
>On 2006-07-05, Frederick Gotham <fg*******@SPAM.comwrote:
>>>
Here's a sample function which converts a string to all uppercase:

#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}
Why is that assert() there? I can see no use for it. Why is the do there?
There's no use for any of that line.

Never mind; I read your other post explaining your logic. You should
probably put a comment in there.

(For others' benefit, the test is because casting to unsigned char is more
of a bandaid than a solution.)
But the test is deeply flawed and the cast is a perfectly reasonable
solution to the problem of toupper requiring a character representable as
unsigned char. It may be a band-aid, but it's a highly effective band-aid.
In fact, I can only think of one case where it won't work - and that is the
case where sizeof(int) is 1, which is well-known to be an
assumption-bending situation in lots of other ways too. The reason it
causes problems with toupper is that it offers no way to distinguish
between (char)-1 and (int)-1. Casting doesn't help in such a case.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 5 '06 #7

Robert Gamble wrote:
Yes, as would be:

while (*p = toupper(*p++) );

Robert Gamble
This is more similar to i = i++; that is stated in the FAQ, URL
<http://www.c-faq.com/ansi/experiment.html.

lovecreatesbeauty

Jul 5 '06 #8
Robert Gamble <rg*******@gmail.comwrote:
>
Yes, as would be:

while (*p = toupper(*p++) );
My reading of the standard suggests that this (whether the above is
undefined behaviour) can't be decided from the standard alone since an
implementation may implement toupper as a function (rather than as a
macro /and/ a function). A minor change:

while (*p = (toupper)(*p++));

makes it well-defined (modulo the signedness of p) for any conforming
implementation.

Of course, it is quite reasonable to lump all constructs that an
implementation /may/ render undefined as undefined but there is, I think,
a technical difference.

--
Ben.
Working towards a posting to comp.lang.c that is 100% correct.
Jul 5 '06 #9
Frederick Gotham wrote:
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}
I appreciate that your are experimenting with the language but I feel a
word of advice is in order. Don't EVER CODE LIKE THAT in fielded code.
Be very explicit about what you want and let the optimizer worry about
things like that.

Also your assert is kinda useless.

Just write it as

while (*p) { *p = toupper(*p); ++p; }

And be done with.

Tom

Jul 5 '06 #10
Ben Bacarisse wrote:
Robert Gamble <rg*******@gmail.comwrote:

Yes, as would be:

while (*p = toupper(*p++) );

My reading of the standard suggests that this (whether the above is
undefined behaviour) can't be decided from the standard alone since an
implementation may implement toupper as a function (rather than as a
macro /and/ a function). A minor change:

while (*p = (toupper)(*p++));

makes it well-defined (modulo the signedness of p) for any conforming
implementation.
No, the construct is undefined even if toupper is a function.

Robert Gamble

Jul 5 '06 #11
Ben Bacarisse wrote:
Robert Gamble <rg*******@gmail.comwrote:
>Yes, as would be:

while (*p = toupper(*p++) );

My reading of the standard suggests that this (whether the above is
undefined behaviour) can't be decided from the standard alone since an
implementation may implement toupper as a function (rather than as a
macro /and/ a function). A minor change:

while (*p = (toupper)(*p++));

makes it well-defined (modulo the signedness of p) for any conforming
implementation.
No it doesn't. Even with toupper being a function (or forcing it to use
the function) there is no sequence point between evaluating p to get the
address to be written to and evaluating *p++ to find the value to be
passed to toupper, only between evaluating *p++ and calling toupper (and
obviously therefore writing the result to *p whatever that happens to be
at the time). So, for instance, the compiler could do:

A1 = p
p++
*A1 = toupper(*A1)

Or it could do
p++
*p = toupper(*p)

Or, since it is undefined behaviour, anything else it wants. However,
something along the lines of the one of the two examples I show above is
most likely.
Of course, it is quite reasonable to lump all constructs that an
implementation /may/ render undefined as undefined but there is, I think,
a technical difference.
I would tend to do that, but in this case it is not relevant.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 5 '06 #12
Flash Gordon <sp**@flash-gordon.me.ukwrote:
Ben Bacarisse wrote:
>Robert Gamble <rg*******@gmail.comwrote:
>>Yes, as would be:

while (*p = toupper(*p++) );

My reading of the standard suggests that this (whether the above is
undefined behaviour) can't be decided from the standard alone since an
implementation may implement toupper as a function (rather than as a
macro /and/ a function). A minor change:

while (*p = (toupper)(*p++));

makes it well-defined (modulo the signedness of p) for any conforming
implementation.

No it doesn't. Even with toupper being a function (or forcing it to use
the function) there is no sequence point between evaluating p to get the
address to be written to and evaluating *p++ to find the value to be
passed to toupper, only between evaluating *p++ and calling toupper
Ah, thank you, I was not thinking straight -- of course the sequence
point from the call comes too late to make any difference.

Since this has made me go read it all over again, and I can hardly loose
more face by posting more daft stuff, I venture to ask what the meaning
of "access" is in the phrase in section 6.5.2.2:

"If an attempt is made to modify the result of a function call or
to access it after the next sequence point, the behavior is
undefined."

It can't mean "use" since that would seem to render a simple
expression such as f() + g() undefined. Can somone give an example of
such an UB-causing access?

--
Ben.
Jul 5 '06 #13
In article <44**********************@news.zen.co.uk>
Ben Bacarisse <sp**@bsb.me.ukwrote:
>Since this has made me go read it all over again, and I can hardly lose
more face by posting more daft stuff, I venture to ask what the meaning
of "access" is in the phrase in section 6.5.2.2:

"If an attempt is made to modify the result of a function call or
to access it after the next sequence point, the behavior is
undefined."

It can't mean "use" since that would seem to render a simple
expression such as f() + g() undefined. Can somone give an example of
such an UB-causing access?
This refers to code like the following:

#include <stdio.h>

struct S { int a[10]; };

struct S new_s(void) {
struct S val;
int i;

for (i = 0; i < 10; i++)
val.a[i] = i;
return val;
}

int main(void) {
int *p;

printf("%d\n", new_s().a[2]); /* defined; prints 2 */

new_s().a[2] = 42; /* undefined - modifies result of function call */

p = new_s().a;
printf("%d\n", p[2]); /* undefined - access after sequence point */

return 0;
}
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 5 '06 #14
Tom St Denis wrote:
Frederick Gotham wrote:
> void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}


I appreciate that your are experimenting with the language but I feel a
word of advice is in order. Don't EVER CODE LIKE THAT in fielded code.
Be very explicit about what you want and let the optimizer worry about
things like that.

Also your assert is kinda useless.

Just write it as

while (*p) { *p = toupper(*p); ++p; }

And be done with.

Tom
Thank you! To the point and done with it!

--
Regards,
Stan Milam
================================================== ===========
Charter Member of The Society for Mediocre Guitar Playing on
Expensive Instruments, Ltd.
================================================== ===========
Jul 6 '06 #15
Chris Torek <no****@torek.netwrote:
In article <44**********************@news.zen.co.uk>
Ben Bacarisse <sp**@bsb.me.ukwrote:
>>Since this has made me go read it all over again, and I can hardly lose
more face by posting more daft stuff, I venture to ask what the meaning
of "access" is in the phrase in section 6.5.2.2:

"If an attempt is made to modify the result of a function call or
to access it after the next sequence point, the behavior is
undefined."

It can't mean "use" since that would seem to render a simple
expression such as f() + g() undefined. Can somone give an example of
such an UB-causing access?

This refers to code like the following:

#include <stdio.h>

struct S { int a[10]; };

struct S new_s(void) {
struct S val;
<snip>
return val;
}

int main(void) {
int *p;
<snip>
p = new_s().a;
printf("%d\n", p[2]); /* undefined - access after sequence point */

return 0;
}
Duh! Of course. Access means to read (or modify) an *object* so to
violate this constraint a program must be able to refer to the object
that is the result of the function and not just its value.

Thanks.

--
Ben.
Jul 6 '06 #16

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

Similar topics

3
by: Sensorflo | last post by:
After browsing though many newsgroups articels I'm still not shure how operator precedence, operator associativity, sequence points, side effects go together. Currently I have the following view: ...
2
by: Jeremy Chapman | last post by:
Populating a typed dataset from xml document: I created an xml schema (attached below), generated a typed dataset from it, and then programatically I tried to populate the typed dataset by...
3
by: sugaray | last post by:
Can somebody explain to me what is sequence point ? With few examples would be even better. Thanx for your help.
53
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an...
7
by: akarl | last post by:
Hi all, Why do I get a warning from gcc with the following program? $ cat test.c #include <stdio.h> int f(int n) { return n;
7
by: Kenneth Brody | last post by:
(From something brought up on "Help with array/pointer segmentation fault needed" thread.) Is "?" a sequence point? Or, more directly, is the following defined? /* Will "ptr" be guaranteed...
5
by: Frederick Gotham | last post by:
Here's a sample function which converts a string to all uppercase: #include <cassert> #include <cctype> void StringUp( char *p ) { do assert( *p >= 0 ); while( *p = std::toupper( *p ), *p++...
9
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
2
by: ais523 | last post by:
The program excerpt int i; char c; char* a= {"abc","def","ghi"}; /* ... */ i=0; c=a; obviously invokes undefined behaviour, because i is modified twice
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

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.