Groups | Blog | Home
all groups > dotnet general > august 2005 >

dotnet general : C++ compiler and argument passing with prefix operation


Guru
8/31/2005 11:34:29 PM
Hi ,
I tried the following 4 line of code and answer is what I never expected.I
ran the code using C++ compiler(MS-Studio .net 03) also in Unix env.In fact
the on Unix i got the correct (or expected answer).
-->
#include <cstdio>
#include <iostream>
using namespace std;
void fn(int, int);
main()
{
int a = 5;
fn(a++, ++a);
}
void fn(int a, int b)
{
printf("Fn : a = %d \t b = %d\n", a, b);
//(a,b) resp. is 6,7 on windows ( i thought it shld be 6,6,) and on unix its
5,7
}
Can anyone tell me why i get b=7 in windows. the fact that parameter passing
in windows is right-left and in unix its left-right which makes sense when i
see value of variable "a" in both cases..but why c++ evaluates the value of
"b" twise??

Is my understanding wrong or if anyone has any satisfactory answer..?

Thanx and Rgds
Guru
9/1/2005 4:59:02 AM
yes im sorry to post this question here...
My intention is to check how my c++ compiler evalutes this type of expr and
pass the parameter on function stack.I tried this on previous c++ version
(dunno exact ver #)but MS Studo 6.0 where it gave me the rght answer for
condition
fn(a++, ++a); i.e (6,6) but different for fn(a++,a++) .
u r rght C# does left-rght evaluate and gives proper answer (dont evaluate
the same expression twise as in case of C++)Also When I tested on out Unix
(similar to Unix SVR4 and MetaWare compiler ) and it gave me wat i expect
for left-rght evaluation EVERY time....Was Just thinking why Micrososft
compilers gives different answers??
language specification dont tell us (or compiler writers) what to do in such
condition??
thanx
/g

[quoted text, click to view]
Helge Jensen
9/1/2005 9:43:01 AM


[quoted text, click to view]

This is probably not the correct group for this question, a c++ language
group would be the right choice, but i'll try and answer anyway.

[quoted text, click to view]

fn(a++, ++a);

Have undefined behaviour. C++ does not define the evaluation-order of
side-effects within sequence-points. IIRC C# imposes left-to-right
evaulation, C++ does not.

If you wish to call fn with 5,6 and then increment a twice, i recommend:

fn(a, a+1);
a += 2;

Which have defined behaviour.

How you could expect fn(6,6) to be invoked is unclear to me. In C#, I
would expect fn(5, 7) to be invoked, due to the left-to-right
evaluation-order. I don't think I would use pre/post-fix twice on the
same value though -- just for the readability -- I would go with fn(a, a+2).

--
Helge Jensen
mailto:helge.jensen@slog.dk
sip:helge.jensen@slog.dk
Jon Skeet [C# MVP]
9/1/2005 7:07:05 PM
[quoted text, click to view]

You seem to be trying to say that the "right" answer is the one which
you expect. As Helge wrote, the order isn't defined in C++ - there *is*
no "right" answer.

[quoted text, click to view]

Exactly - the specification doesn't define it, so don't try to rely on
one type of behaviour.

In a way, this is a good thing, as it discourages you from writing such
code in the first place - and code like that is generally very hard to
read.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
AddThis Social Bookmark Button