473,237 Members | 1,269 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,237 software developers and data experts.

Violating Sequence Point?

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++ );
}
Would the "Sequence point rule" be violated if the code were changed to the
following:
#include <cassert>
#include <cctype>

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

Frederick Gotham
Jul 5 '06 #1
5 1225
Frederick Gotham wrote:
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++ );
}
Would the "Sequence point rule" be violated if the code were changed to
the following:
#include <cassert>
#include <cctype>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p++ = std::toupper(*p) );
}
a) Feels like it: you dereference the same pointer twice within an
expression that, as a side effect, changes the pointer. Sounds dangerous.

Now, suppose that carefull analysis of the C++ standard showed the code in
question to be legit. Would you then consider it good code? Would you be
inclined to put that analysis as a comment next to the code so that a
maintainer does not feel the need to check carefully?

b) Why not avoid borderline cases and comma operator trickery altogether.
Why not put every action on a line of its own:

#include <cctype>

void StringUp( char * p ) {
while ( *p != 0 ) {
*p = std::toupper( *p );
++ p;
}
}

c) I do not understand the assert( *p >= 0 ) in your code. If char is
unsigned, it will never fail, if char is signed, it may. However there is
no reason why StringUp() should not be called on strings containing
negative characters. I cannot see a reason to make that restriction part of
the contract.
Best

Kai-Uwe Bux
Jul 5 '06 #2

"Kai-Uwe Bux" <jk********@gmx.netskrev i meddelandet
news:e8**********@murdoch.acc.Virginia.EDU...
Frederick Gotham wrote:
>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++ );
}
Would the "Sequence point rule" be violated if the code were
changed to
the following:
#include <cassert>
#include <cctype>

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

a) Feels like it: you dereference the same pointer twice within an
expression that, as a side effect, changes the pointer. Sounds
dangerous.

Now, suppose that carefull analysis of the C++ standard showed the
code in
question to be legit. Would you then consider it good code? Would
you be
inclined to put that analysis as a comment next to the code so that
a
maintainer does not feel the need to check carefully?

b) Why not avoid borderline cases and comma operator trickery
altogether.
Why not put every action on a line of its own:

#include <cctype>

void StringUp( char * p ) {
while ( *p != 0 ) {
*p = std::toupper( *p );
++ p;
}
}

c) I do not understand the assert( *p >= 0 ) in your code. If char
is
unsigned, it will never fail, if char is signed, it may. However
there is
no reason why StringUp() should not be called on strings containing
negative characters. I cannot see a reason to make that restriction
part of
the contract.
It is because std::toupper() takes an int parameter, and doesn't work
for negative values. Simple and transparent code, isn't it? :-)

Also, what about

StringUp("Goodbye World");

What happens?
Bo Persson
Jul 5 '06 #3
Bo Persson wrote:
>>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++ );
}
Would the "Sequence point rule" be violated if the code were
changed to
the following:
#include <cassert>
#include <cctype>

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

a) Feels like it: you dereference the same pointer twice within an
expression that, as a side effect, changes the pointer. Sounds
dangerous.

Now, suppose that carefull analysis of the C++ standard showed the
code in
question to be legit. Would you then consider it good code? Would
you be
inclined to put that analysis as a comment next to the code so that
a
maintainer does not feel the need to check carefully?

b) Why not avoid borderline cases and comma operator trickery
altogether.
Why not put every action on a line of its own:

#include <cctype>

void StringUp( char * p ) {
while ( *p != 0 ) {
*p = std::toupper( *p );
++ p;
}
}

c) I do not understand the assert( *p >= 0 ) in your code. If char
is unsigned, it will never fail, if char is signed, it may. However
there is no reason why StringUp() should not be called on strings
containing negative characters. I cannot see a reason to make that
restriction part of the contract.

It is because std::toupper() takes an int parameter, and doesn't work
for negative values. Simple and transparent code, isn't it? :-)
However, the solution would be to cast the value to unsigned char, not to
assume (assert) that negative value won't ever be there.
Also, what about

StringUp("Goodbye World");

What happens?
Then an old heritage from C jumps in that unfortunately allows this call to
compile. If you're lucky, the compiler gives you a warning.

Jul 5 '06 #4
Bo Persson wrote:
>
"Kai-Uwe Bux" <jk********@gmx.netskrev i meddelandet
news:e8**********@murdoch.acc.Virginia.EDU...
>Frederick Gotham wrote:
>>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++ );
}
Would the "Sequence point rule" be violated if the code were
changed to
the following:
#include <cassert>
#include <cctype>

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

a) Feels like it: you dereference the same pointer twice within an
expression that, as a side effect, changes the pointer. Sounds
dangerous.

Now, suppose that carefull analysis of the C++ standard showed the
code in
question to be legit. Would you then consider it good code? Would
you be
inclined to put that analysis as a comment next to the code so that
a
maintainer does not feel the need to check carefully?

b) Why not avoid borderline cases and comma operator trickery
altogether.
Why not put every action on a line of its own:

#include <cctype>

void StringUp( char * p ) {
while ( *p != 0 ) {
*p = std::toupper( *p );
++ p;
}
}

c) I do not understand the assert( *p >= 0 ) in your code. If char
is
unsigned, it will never fail, if char is signed, it may. However
there is
no reason why StringUp() should not be called on strings containing
negative characters. I cannot see a reason to make that restriction
part of
the contract.

It is because std::toupper() takes an int parameter, and doesn't work
for negative values. Simple and transparent code, isn't it? :-)
Rats, I always confuse the cctype and locale versions.
Also, what about

StringUp("Goodbye World");

What happens?
Good points. So lets try this:

#include <locale>

template < typename CharT >
void StringUp( CharT * p ) {
std::locale loc;
while ( *p != 0 ) {
*p = std::toupper( *p, loc );
++p;
}
}
The templating apparently also fixes the StringUp( "hello world!" ) problem.
Best

Kai-Uwe Bux
Jul 5 '06 #5
"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:cb*******************@news.indigo.ie...
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++ );
}
Would the "Sequence point rule" be violated if the code were changed to
the
following:
#include <cassert>
#include <cctype>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p++ = std::toupper(*p) );
}
Yes, because there is no sequence point between the left- and right-hand
sides of an assignment. So even though *p must be evaluated before calling
std::toupper (because you can't call a function before you know the values
of its arguments), there is still no defined evaluation order for *p++ and
std::toupper(*p).

It seems clear what you're trying to accomplish: You want to take a pointer
to the initial character of a null-terminated string and make every
character uppercase. But for some reason, you're putting the terminating
null character through toupper as well, even though it makes no sense to do
so.

I would do it this way instead:

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

Alternatively:

for (; *p; ++p)
*p = std::toupper(*p);

because it makes clear the iterative nature of the loop.
Jul 5 '06 #6

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...
15
by: Frederick Gotham | last post by:
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++ );...
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
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
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: 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...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.