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
Gaurav 3 1854
Guru wrote: 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).
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.
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??
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:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
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
"Helge Jensen" wrote:
Guru wrote:
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).
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.
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??
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:he**********@slog.dk sip:he**********@slog.dk -=> Sebastian cover-music: http://ungdomshus.nu <=-
Guru <Gu**@discussions.microsoft.com> wrote: 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++) .
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.
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??
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 - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sacha Faust |
last post by:
I have an abstract class, RuleResponse, and then create a new class base on
it, RuleResponseSequence, and override the ++ operater.
If I try to cast a RuleResponse as a RuleResponseSequence and...
|
by: junky_fellow |
last post by:
N869, Page 47,
"Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type ''array of type'' is...
|
by: Guru |
last post by:
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...
|
by: dave_dp |
last post by:
Hi, I have just started learning C++ language..
I've read much even tried to understand the way standard says but still
can't get the grasp of that concept.
When parameters are passed/returned...
|
by: Miroslaw Makowiecki |
last post by:
Where can I download Comeau compiler as a trial version?
Thanks in advice.
|
by: swapnaoe |
last post by:
Hi,
In http://elearning.embnet.org/file.php/43/ctutorial/Postfix-and-prefix----and---.html I read about postfix and prefix unary operators.
It said " If the increment or decrement operator is...
|
by: tkietzma |
last post by:
Hello world,
I have some code, which happily runs on Ubuntu. Now, I have to port
this code to OS X. However, on compiling I get (hundreds of) errors
like:
...
|
by: seaside |
last post by:
I have a method
function appendChildNode( AST $aNewChild ) {
...
}
<<<
where AST is a class. If I pass null, PHP renders this message:
Catchable fatal error: Argument 1 passed to...
|
by: Dave |
last post by:
I'm having a hard time tying to build gcc 4.3.1 on Solaris using the GNU
compilers. I then decided to try to use Sun's compiler. The Sun Studio
12 compiler reports the following code, which is in...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |