473,385 Members | 2,069 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,385 software developers and data experts.

Creating sequence points using macros

The program excerpt

int i;
char c;
char* a[]= {"abc","def","ghi"};
/* ... */
i=0;
c=a[i++][i++];

obviously invokes undefined behaviour, because i is modified twice
between sequence points. I was wondering if it would be possible to
define some sort of macro to generate a sequence point, so that
c=seq(a[i++])[i++];
could be written and lead to the behaviour being fully defined. (The
idea is that a sequence point would be introduced after the i++ on the
left was evaluated, forcing i to be incremented before the second i++
even runs and avoiding the UB.) Apart from splitting the statement
into two statements (which would probably be the best way to do this
in practice), is there any portable C89 or C99 way to define seq? (By
the way, if it didn't have to work with multiple data types, would a
function that returns its own argument work?)
--
ais523

Mar 28 '07 #1
2 1620
ais523 wrote:
The program excerpt

int i;
char c;
char* a[]= {"abc","def","ghi"};
/* ... */
i=0;
c=a[i++][i++];

obviously invokes undefined behaviour, because i is modified twice
between sequence points. I was wondering if it would be possible to
define some sort of macro to generate a sequence point, so that
c=seq(a[i++])[i++];
could be written and lead to the behaviour being fully defined. (The
idea is that a sequence point would be introduced after the i++ on the
left was evaluated, forcing i to be incremented before the second i++
even runs and avoiding the UB.) Apart from splitting the statement
into two statements (which would probably be the best way to do this
in practice),
Or
c = a[i][i+1], i += 2;

One statement.
is there any portable C89 or C99 way to define seq?
As a macro? I don't think so.
(By
the way, if it didn't have to work with multiple data types, would a
function that returns its own argument work?)
(fx:trance)

I don't think so. If `seq` is a function, in `seq( a[i++] )[i++]`
there's no reason that the second `i++` has to be evaluated after
the function call (ie after the sequence point), so it's just as
undefined as if the sequence point wasn't there.

But I'm not a sequence-point expert.

--
The second Jena user conference! http://hpl.hp.com/conferences/juc2007/
Nit-picking is best done among friends.

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Mar 28 '07 #2
On Mar 28, 3:55 pm, Chris Dollin <chris.dol...@hp.comwrote:
ais523 wrote:
(snip)
I was wondering if it would be possible to
define some sort of macro to generate a sequence point, so that
c=seq(a[i++])[i++];
could be written and lead to the behaviour being fully defined.
(snip)
(By
the way, if it didn't have to work with multiple data types, would a
function that returns its own argument work?)

(fx:trance)

I don't think so. If `seq` is a function, in `seq( a[i++] )[i++]`
there's no reason that the second `i++` has to be evaluated after
the function call (ie after the sequence point), so it's just as
undefined as if the sequence point wasn't there.

But I'm not a sequence-point expert.
That's pretty much what I'd thought, but I wanted to make sure anyway.
--
ais523

Mar 28 '07 #3

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: ...
4
by: Timothy Madden | last post by:
Hello I've read a long time ago in the MSDN that C++ language defines no sequence points Now I read in the 1998 ISO standard a small list of sequence points in C++ Does C++ defines sequence...
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;
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...
1
by: lovecreatesbea... | last post by:
---quoting--- Annex C (informative) Sequence points 1 The following are the sequence points described in 5.1.2.3: - The end of a full expression: an initializer (6.7.8); the expression in an...
19
by: Tony | last post by:
I'm working on project that plays movies using Windows Media Player and I'm controlling everything with JavaScript. Per the client I only need to support IE 6 or greater which happens to make...
7
by: Jrdman | last post by:
hi According to the standard these are how we define sequence points: *the call to a function ,after the arguments have been evaluated *the end of the first operand of the following...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.