473,569 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1238
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("Goodb ye 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("Goodb ye 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("Goodb ye 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******** ***********@new s.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
2377
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: An expression a = b() + c() * d++; can be transformed with the rules of operator associativity and operator precedence into a tree
2
9501
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 calling its ReadXml method. I keep getting a constraint exception. I have validated that my xml matches the schema using xmlspy. I've included the...
3
7090
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
4036
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 object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to...
7
2066
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
1598
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 to have been assigned before the "?" * part is evaluated? */
15
1638
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
2499
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 uses errno, modified by right operand): (log(x) - mu) * (log(x) - mu) Code has unspecified behavior. Order of evaluation of function parameters or...
2
1631
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
7612
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...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
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...
1
7672
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...
0
7968
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...
0
6283
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...
0
3653
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...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.