Discussion:
www.gnu.org/software/gcc/bugs/#nonbugs_c (with ++ / --)
Patrick Serru
2014-07-03 15:41:56 UTC
Permalink
Hi All,

I have a philosophical problem with the gcc compiler. At school, I seem to
have learned that Expression were evaluated from left to right when they are
separated by a comma. I do not understand why this is not applied when it
comes to passing parameters to a function. So I find that if I write:
void foo (int a, int b, int c);
then:
foo (a, b, a + = 1);
1st and 3rd arguments have the same value.

You may say, the order of which we told you about in school relates an
expression that is not a sequence of parameters (?). Nothing defined
implementation of the passage of a function settings. But it turns out that
in the case of gcc, they are pushed onto the stack from right to left. As it
has been since the dawn of time in Savannah, it is definitely impossible to
change this state of affairs in gnou. Thus be it!

So my question is: reverse the order of parameters (shock me, but) make
functional! What about you? Portability?

Thank you for the attention you have brought to this email.
Patrick Serru


J'ai un probléme philosophique avec le compilateur gcc. À l'école, il me
semble avoir appris, que les expression étaient évaluée de gauche à droite
quand elles sont séparées par une virgule. Je ne comprends donc pas pourquoi
ceci n'est pas appliqué quand il s'agit du passage de paramètres à une
fonction. Donc, je constate que si j'écris:
void truc (int a, int b, int c);
puis:
truc(a, b, a+=1);
le 1er et le 3ème argument ont la même valeur.

Vous direz peut-être: l'ordre dont on vous a parlé à l'école concerne une
expression, ce que n'est pas une suite de paramètres (?). Rien ne défini
l'implémentation le passage des paramètres d'une fonction. Or, il se trouve
que dans le cas de gcc, ils sont poussés dans la pile de droite à gauche.
Comme il en est ainsi depuis la nuit des temps dans la savane, il est
définitivement impossible de changer cet état de fait chez gnou.
Ainsi-soit-il!

Alors, ma question est: inverser l'ordre des paramètres (me choquerait,
mais) rendrait fonctionnel ! Qu'en pensez-vous ? Portabilité ?

Merci pour l'attention que vous aurez porté à ce mail.
Patrick Serru
Jonathan Wakely
2014-07-03 16:00:02 UTC
Permalink
Post by Patrick Serru
Hi All,
I have a philosophical problem with the gcc compiler. At school, I seem to
have learned that Expression were evaluated from left to right when they are
separated by a comma. I do not understand why this is not applied when it
void foo (int a, int b, int c);
foo (a, b, a + = 1);
1st and 3rd arguments have the same value.
You may say, the order of which we told you about in school relates an
expression that is not a sequence of parameters (?).
Correct.

Comma operators within an expression affect evaluation order, but the
commas between function arguments are not comma operators.

As it says in the C FAQ (http://c-faq.com/expr/comma.html) if the
commas separating function arguments were comma operators then no
function could receive more than one argument.

Just because the same character is used doesn't make it the same thing.
Post by Patrick Serru
Nothing defined
implementation of the passage of a function settings. But it turns out that
in the case of gcc, they are pushed onto the stack from right to left.
I think it depends on the hardware architecture.
Post by Patrick Serru
As it
has been since the dawn of time in Savannah, it is definitely impossible to
change this state of affairs in gnou. Thus be it!
So my question is: reverse the order of parameters (shock me, but) make
functional! What about you? Portability?
I don't understand the question.
Fabian Cenedese
2014-07-04 07:33:09 UTC
Permalink
Post by Jonathan Wakely
Post by Patrick Serru
Nothing defined
implementation of the passage of a function settings. But it turns out that
in the case of gcc, they are pushed onto the stack from right to left.
I think it depends on the hardware architecture.
Post by Patrick Serru
As it
has been since the dawn of time in Savannah, it is definitely impossible to
change this state of affairs in gnou. Thus be it!
So my question is: reverse the order of parameters (shock me, but) make
functional! What about you? Portability?
I don't understand the question.
He probably means that if he reverses the argument order (like a+=1, b, a) then
he'd get the desired result. But as you mentioned it probably depends on the
hardware and compiler so it is not portable.

I'd say this is undefined behaviour and could be solved like
c=a;
foo(c, b, a+=1); // order of evaluation not important

bye Fabi
Jonathan Wakely
2014-07-04 08:14:10 UTC
Permalink
Post by Fabian Cenedese
Post by Jonathan Wakely
Post by Patrick Serru
Nothing defined
implementation of the passage of a function settings. But it turns out that
in the case of gcc, they are pushed onto the stack from right to left.
I think it depends on the hardware architecture.
Post by Patrick Serru
As it
has been since the dawn of time in Savannah, it is definitely impossible to
change this state of affairs in gnou. Thus be it!
So my question is: reverse the order of parameters (shock me, but) make
functional! What about you? Portability?
I don't understand the question.
He probably means that if he reverses the argument order (like a+=1, b, a) then
he'd get the desired result. But as you mentioned it probably depends on the
hardware and compiler so it is not portable.
I'd say this is undefined behaviour and could be solved like
c=a;
foo(c, b, a+=1); // order of evaluation not important
Agreed, that is a safe and portable way to do it.

Loading...