By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,011 Members | 1,000 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,011 IT Pros & Developers. It's quick & easy.

Creating sequence points using macros

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.