question about calling func( z, z++, z++ )

C

Chad

In the following code, there is a something like func( z, z++, z++ )

[cdalten@localhost oakland]$ more luc.c
#include <stdio.h>

int func( int a, int b, int c )
{
printf( "a is %d and b is %d and c is %d\n", a, b, c );
return(c);
}

int main( void )
{
int z = 9;
func( z, z++, z++ );

return 0;
}
[cdalten@localhost oakland]$ gcc -Wall luc.c -o luc
luc.c: In function ‘main’:
luc.c:12: warning: operation on ‘z’ may be undefined
luc.c:12: warning: operation on ‘z’ may be undefined
[cdalten@localhost oakland]$ ./luc
a is 11 and b is 10 and c is 9
[cdalten@localhost oakland]$

Is func( z, z++, z++) a constraint violation?

Chad
 
N

Nick Keighley

Come on Keith.  Do you really think a genuine newbie, unaware that

f(z,z++);

is UB is going to use the term "constraint violation"?

well he knew it might be wrong and now he knows its wrong and
what sort of wrong
 
G

gwowen

No, but a near-newbie, aware of the existence of constraint violations,

Really? A near newbie who writes in standard-ese?
No-one unconversant with the standard talks about "constraint
violations",
they call them "programming/syntax errors" or "bad code" or
"disallowed code".

So sure, Chad could be someone who has read the standard well enough
to know
the terminology, but not understood it well enough to recognise f(z,z+
+).
Oh, and has not followed this group long enough to see the 30 times
this
question has been asked in the last 3 months.

That's not completely impossible, but, given the usual level of debate
in c.l.c.,
does it not strike you as considerably less likely than the fact its a
wind-up?
 
C

chad

Really? A near newbie who writes in standard-ese?
No-one unconversant with the standard talks about "constraint
violations",
they call them "programming/syntax errors" or "bad code" or
"disallowed code".

So sure, Chad could be someone who has read the standard well enough
to know
the terminology, but not understood it well enough to recognise f(z,z+
+).

I have some idea what a constraint violation is. However, I didn't
know it well enough to recognize if func( z, z++, z++ ) was or wasn't
a constraint violation. Hence why I had asked.
Oh, and has not followed this group long enough to see the 30 times
this
question has been asked in the last 3 months.

I've been sporadically following this group for the past few years.
And when I do read it, I normally just do a brief scan over the recent
posts.
 
E

Eric Sosman

gwowen said:
Come on Keith. Do you really think a genuine newbie, unaware that

f(z,z++);

is UB is going to use the term "constraint violation"?

If he's the same "Chad" who's been here on and off for
several years now, he's not a "genuine newbie."
 
J

jameskuyper

gwowen said:
Really? A near newbie who writes in standard-ese?
No-one unconversant with the standard talks about "constraint
violations",
they call them "programming/syntax errors" or "bad code" or
"disallowed code".

You don't have to be conversant with the standard; reading this
newsgroup is sufficient to acquaint someone with that terminology.
So sure, Chad could be someone who has read the standard well enough
to know
the terminology, but not understood it well enough to recognise f(z,z+
+).


Believe it or not, there's a large chunk of the learning curve between
becoming aware of the concept of a "constraint violation", and
becoming sufficiently familiar with the standard to reliably guess
whether a given problem is a constraint violation, a syntax error, or
undefined behaviour. People call me a language lawyer; I myself only
reached the point, a year or two ago, where I could answer such
questions with reasonable reliability without first double-checking
what the standard says. Even now I still make occasional mistakes.
That's not completely impossible, but, given the usual level of debate
in c.l.c.,
does it not strike you as considerably less likely than the fact its a
wind-up?

It's possible, but I have nowhere near as much certainty about that
point as you have.
 
K

Keith Thompson

chad said:
I have some idea what a constraint violation is. However, I didn't
know it well enough to recognize if func( z, z++, z++ ) was or wasn't
a constraint violation. Hence why I had asked.
[...]

Keep in mind that a constraint violation is something that must be
diagnosed at compile time. A clever compiler *could* recognize that
func( z, z++, z++ ) violates the rules, but it would require analysis
of a kind that compilers are not generally required to perform.

Consider
func( *p0, (*p1)++, (*p2)++ )
where p0, p1, and p2 are pointers. If they all point to different
objects, this is fine. If any two of them point to the same object,
the behavior is undefined, for exactly the same reason that the
behavior of
func( z, z++, z++ )
is undefined. Since the values of p0, p1, and p2 cannot in general be
known at compile time, the standard doesn't require the compiler to
recognize that there's a problem -- hence the behavior is undefined.
 
K

Keith Thompson

Frank said:
I have some idea what a constraint violation is. However, I didn't
know it well enough to recognize if func( z, z++, z++ ) was or wasn't
a constraint violation. Hence why I had asked. [...]
[...]

There's no sequence point to be between the modifications of the object.

Keith, is it illegal to have a comma operator as any of the commas above?

Not illegal exactly. The commas in func( z, z++, z++ ) simply aren't
comma operators. The syntax of a function call is:

postfix-expression
(
zero or more assignment-expressions, separated by commas
)

If you want a comma operator in a function call, you can parenthesize
it:

func( (a, b), (c, d), (e, f) )
 
F

Frank

In Dread Ink, the Grave Hand of Keith Thompson Did Inscribe:
chad said:
I have some idea what a constraint violation is. However, I didn't
know it well enough to recognize if func( z, z++, z++ ) was or wasn't
a constraint violation. Hence why I had asked.
[...]

Keep in mind that a constraint violation is something that must be
diagnosed at compile time. A clever compiler *could* recognize that
func( z, z++, z++ ) violates the rules, but it would require analysis
of a kind that compilers are not generally required to perform.

Consider
func( *p0, (*p1)++, (*p2)++ )
where p0, p1, and p2 are pointers. If they all point to different
objects, this is fine. If any two of them point to the same object,
the behavior is undefined, for exactly the same reason that the
behavior of
func( z, z++, z++ )
is undefined. Since the values of p0, p1, and p2 cannot in general be
known at compile time, the standard doesn't require the compiler to
recognize that there's a problem -- hence the behavior is undefined.

Chad and I seem to be persons who return to clc occasionally as
autodidacts. I think I remember him.

There's no sequence point to be between the modifications of the object.

Keith, is it illegal to have a comma operator as any of the commas above?
 
P

Peter Nilsson

Keith Thompson said:
... Keep in mind that a constraint violation is something
that must be diagnosed at compile time. ...

A few nits:

- Interpreters don't do compilation.

- 6.10 (Preprocessing directives) has quite a few constraints.

- I don't think the standard specifies _when_ a constraint is
to be issued.

I'm a bit surprised that unresolved external identifiers are
not constraint violations, but there you go.
 
F

Frank

In Dread Ink, the Grave Hand of Keith Thompson Did Inscribe:
Frank said:
I have some idea what a constraint violation is. However, I didn't
know it well enough to recognize if func( z, z++, z++ ) was or wasn't
a constraint violation. Hence why I had asked.
[...]
[...]

There's no sequence point to be between the modifications of the object.

Keith, is it illegal to have a comma operator as any of the commas above?

Not illegal exactly. The commas in func( z, z++, z++ ) simply aren't
comma operators. The syntax of a function call is:

postfix-expression
(
zero or more assignment-expressions, separated by commas
)

If you want a comma operator in a function call, you can parenthesize
it:

func( (a, b), (c, d), (e, f) )

I see. So func is still gonna have three arguments.
--
Frank

I once asked the most fabulous couple I know, Madonna and Guy Ritchie, how
they kept things fresh despite having been married for almost seven months.
'It's a job, Al,' Guy told me. 'We work at it every day.'
~~ Al Franken,
 
J

James Kuyper

Phil said:
Yes, it indeed violates the contraints that guarantee definedness
of the behaviour.

Could you identify those constraints by section and paragraph?
 
K

Keith Thompson

Phil Carmody said:
Yes, it indeed violates the contraints that guarantee definedness
of the behaviour.

"Constraint" has a specific meaning in C. func(z, z++, z++)
violates no constraint.
 
T

Tim Rentsch

Peter Nilsson said:
A few nits:

- Interpreters don't do compilation.

- 6.10 (Preprocessing directives) has quite a few constraints.

- I don't think the standard specifies _when_ a constraint is
to be issued.

Not directly, but the Standard does distinguish translation
and execution. If a constraint violation occurs during
translation, it seems like a diagnostic must be issued
at that point, since if it weren't the implementation
might not be able to fulfill its obligation to issue
the diagnostic. There is no requirement that execution
must be requested, but there is a requirement that
a diagnostic be issued.
 
P

Phil Carmody

James Kuyper said:
Phil said:
Keith Thompson said:
[...]
Is func( z, z++, z++) a constraint violation?
No, but its behavior is undefined.

Yes, it indeed violates the contraints that guarantee definedness of
the behaviour.

Could you identify those constraints by section and paragraph?

Can you identify the section which indicates that it's UB?

Phil
 
P

Phil Carmody

Keith Thompson said:
Phil Carmody said:
Keith Thompson said:
[...]
Is func( z, z++, z++) a constraint violation?

No, but its behavior is undefined.

Yes, it indeed violates the contraints that guarantee definedness
of the behaviour.

"Constraint" has a specific meaning in C. func(z, z++, z++)
violates no constraint.

It also has a specific meaning in English.

If you left your Plaugher next to an electric heater - would it char?

Phil
 
J

jameskuyper

Phil said:
James Kuyper said:
Phil said:
[...]
Is func( z, z++, z++) a constraint violation?
No, but its behavior is undefined.

Yes, it indeed violates the contraints that guarantee definedness of
the behaviour.

Could you identify those constraints by section and paragraph?

Can you identify the section which indicates that it's UB?

Yes: 6.5p2.
Your turn.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top