473,386 Members | 1,867 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,386 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 1230
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
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
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,...
0
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...

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.