undefined behavior?

L

Loic Domaigne

Hello everybody,

Let's assume that I have the following prototype:
void myf(int x, ...) ;

The function myf() is then called as follows:

int i=0;
f(a[i++],a[i++]); // a is an array

This sounds to me like an undefined behavior, am I right?

Thanks in advance,
Loic.
--
 
M

mail2sandeepnl

Hello everybody,

Let's assume that I have the following prototype:
void myf(int x, ...) ;

The function myf() is then called as follows:

int i=0;
f(a[i++],a[i++]); // a is an array

This sounds to me like an undefined behavior, am I right?

Thanks in advance,
Loic.
--

yes , order of evaluation in function parameters is compiler dependent
 
R

Richard Bos

Let's assume that I have the following prototype:
void myf(int x, ...) ;

The function myf() is then called as follows:

int i=0;
f(a[i++],a[i++]); // a is an array

This sounds to me like an undefined behavior, am I right?

yes , order of evaluation in function parameters is compiler dependent

It's worse than that. It's not just implementation-defined behaviour,
it's full-blown _un_-defined behaviour. If it only depended on the order
of evaluation, you'd have two possible outcomes: one way 'round, or the
other way 'round. But it's undefined, so anything may happen, from what
the naive programmer believes "should" happen to a complete crash, and
worse.
The prototype of the function doesn't matter here, BTW. You modify the
value of i twice between sequence points. _That_ causes UB, no matter
what else.

Richard
 
J

Joachim Schmitz

Richard Bos said:
Let's assume that I have the following prototype:
void myf(int x, ...) ;

The function myf() is then called as follows:

int i=0;
f(a[i++],a[i++]); // a is an array

This sounds to me like an undefined behavior, am I right?

yes , order of evaluation in function parameters is compiler dependent

It's worse than that. It's not just implementation-defined behaviour,
it's full-blown _un_-defined behaviour. If it only depended on the order
of evaluation, you'd have two possible outcomes: one way 'round, or the
other way 'round. But it's undefined, so anything may happen, from what
the naive programmer believes "should" happen to a complete crash, and
worse.
The prototype of the function doesn't matter here, BTW. You modify the
value of i twice between sequence points. _That_ causes UB, no matter
what else.
the comma doesn't act as a sequence point here inside an argument list, like
is otherwise would?

Bye, Jojo
 
R

Richard Bos

Joachim Schmitz said:
Richard Bos said:
Let's assume that I have the following prototype:
void myf(int x, ...) ;

The function myf() is then called as follows:

int i=0;
f(a[i++],a[i++]); // a is an array

This sounds to me like an undefined behavior, am I right?

yes , order of evaluation in function parameters is compiler dependent

It's worse than that. It's not just implementation-defined behaviour,
it's full-blown _un_-defined behaviour. If it only depended on the order
of evaluation, you'd have two possible outcomes: one way 'round, or the
other way 'round. But it's undefined, so anything may happen, from what
the naive programmer believes "should" happen to a complete crash, and
worse.
The prototype of the function doesn't matter here, BTW. You modify the
value of i twice between sequence points. _That_ causes UB, no matter
what else.
the comma doesn't act as a sequence point here inside an argument list, like
is otherwise would?

The comma only introduces a sequence point when it's the comma
_operator_. These are separators, a different part of the grammar
entirely. So are the commas in, for example, this declaration:

int i,j, *ip, ia[14];

Richard
 
S

santosh

Joachim said:
Richard Bos said:
Let's assume that I have the following prototype:
void myf(int x, ...) ;

The function myf() is then called as follows:

int i=0;
f(a[i++],a[i++]); // a is an array

This sounds to me like an undefined behavior, am I right?

yes , order of evaluation in function parameters is compiler
dependent

It's worse than that. It's not just implementation-defined behaviour,
it's full-blown _un_-defined behaviour. If it only depended on the
order of evaluation, you'd have two possible outcomes: one way
'round, or the other way 'round. But it's undefined, so anything may
happen, from what the naive programmer believes "should" happen to a
complete crash, and worse.
The prototype of the function doesn't matter here, BTW. You modify
the value of i twice between sequence points. _That_ causes UB, no
matter what else.
the comma doesn't act as a sequence point here inside an argument
list, like is otherwise would?

No.
 
L

Loic Domaigne

Hello everybody,

Let's assume that I have the following prototype:
void myf(int x, ...) ;

The function myf() is then called as follows:

int i=0;
f(a[i++],a[i++]); // a is an array

This sounds to me like an undefined behavior, am I right?

Thanks in advance,
Loic.
--

Thanks everybody for your answers.

Cheers,
Loic.
 
D

David Thompson

f(a[i++],a[i++]); // a is an array
the comma doesn't act as a sequence point here inside an argument list, like
is otherwise would?

The comma only introduces a sequence point when it's the comma
_operator_. These are separators, a different part of the grammar
entirely. So are the commas in, for example, this declaration:

int i,j, *ip, ia[14];
(Both) true as stated. However, a 'toplevel' comma in a declaration
coincides with either the end of a full-declarator (as here) or of an
initializer, and either of those IS a sequence point.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top