|
-
May 14th, 2007, 04:23 AM
#1
A question for C++ expert on ++a and a++
This is the question for C++ expert, what will be the output of following codes and why?
Thanks a lot in advance!
Code:
#include <iostream>
int main()
{
int a = 5;
int d = a++;
a = 5;
int b = a++ * ++a;
a = 5;
int c = ++a * a++;
a = 5;
int e = (a++) * (1);
std::cout << "b = " << b << std::endl;
std::cout << "c = " << c << std::endl;
std::cout << "d = " << d << std::endl;
std::cout << "e = " << e << std::endl;
}
-
May 14th, 2007, 04:32 AM
#2
Re: A question for C++ expert on ++a and a++
I am not an expert ( but who is ? ).
The question about "what" can easily be answered by just running the program.
The question about "why" cannot fully be answered because of undefined behaviour.
Kurt
-
May 14th, 2007, 04:37 AM
#3
Re: A question for C++ expert on ++a and a++
Yes, but I prefer you to think about the answer before run it in your platform.
I cannot answer why the output it is, so I think some expert can explain it. And thanks to explain more about "undefined behaviour", you mean the result will be different based on different platform and compiler?
-
May 14th, 2007, 04:50 AM
#4
Re: A question for C++ expert on ++a and a++
Why 'undefined behaviour'? I think it's clear, isn't it?
Post-increment is performed after the expression is evaluated, and pre-increment before.
For example:First evaluated is ++a, so it becomes 6. Then it's performed b = a * a = 36, then a++, so becomes 7. Where is the 'undefined behaviour'?
Albert.
Please, correct me. I'm just learning.... and sorry for my english :-)
-
May 14th, 2007, 04:59 AM
#5
Re: A question for C++ expert on ++a and a++
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
May 14th, 2007, 05:00 AM
#6
Re: A question for C++ expert on ++a and a++
As far as I know * is not a sequence point it is possible that either ++a or a++ is evaluated first.
Kurt
-
May 14th, 2007, 05:06 AM
#7
Re: A question for C++ expert on ++a and a++
yes, I guess it is the reason. Thanks a lot guys!
Code:
int operator *(const int & lhs, const int & rhs);
in the function operator , no compiler can guarantee that lhs will be processed before rhs, maybe that is why it is called"undefined behaviour".
Kurt
-
May 14th, 2007, 05:23 AM
#8
Re: A question for C++ expert on ++a and a++
Correct.
btw: This is one of the reasons to NEVER overload operators "&&" and "||". By specification the native versions "short-curcuit", and any overload will evaluate BOTH!!!
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 14th, 2007, 05:23 AM
#9
Re: A question for C++ expert on ++a and a++
 Originally Posted by Hobson
Ok, I thought it worked in different way. Thanks for explanation.
Albert.
Please, correct me. I'm just learning.... and sorry for my english :-)
-
May 14th, 2007, 05:34 AM
#10
Re: A question for C++ expert on ++a and a++
First of all is good to return 0 because you use int main() and a value must be returned.++a increase the a by 1 and then use the new value of a in the expression.a++ use the current value in the expression and then increase the a by 1
-
May 14th, 2007, 05:51 AM
#11
Re: A question for C++ expert on ++a and a++
 Originally Posted by ZuK
As far as I know * is not a sequence point it is possible that either ++a or a++ is evaluated first.
No.. that would be unspecified behaviour (unspecified is not exactly undefined in the standards terms). Order of evaluation is unspecified. And that to the OP's code causes undefined behaviour because it is only defined that the side-effects of the increments (updating the variable storage included) will happen at a sequence point (here the semi-colon ; ). The evaluations of sub-expressions for those expressions could be interlaced with each other. Which means for this statement - int b = a++ * ++a, the following can happen in any order (events *a interfering with *b):
1a. read a for a++
2a. evaluate a++
3a. update storage of a after a++
1b. read a for ++a
2b. evaluate ++a
3b. update storage of a after ++a
Those operations themselves in their sub-expressions may be evaluated in order 1, 2, 3 but there can interference with each other. A write and a read or a write and a write without a sequence point causes undefined behaviour as per the standards. Hence, even a = a++; is undefined (two writes to the same variable without a sequence point in between).
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 14th, 2007, 05:53 AM
#12
Re: A question for C++ expert on ++a and a++
 Originally Posted by TheCPUWizard
btw: This is one of the reasons to NEVER overload operators "&&" and "||". By specification the native versions "short-curcuit", and any overload will evaluate BOTH!!!
No, this cannot be the reason as any overload would be a function call and function call is a sequence point. The result would be unspecified but not undefined.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 14th, 2007, 05:54 AM
#13
Re: A question for C++ expert on ++a and a++
 Originally Posted by tzimis
First of all is good to return 0 because you use int main() and a value must be returned.++a increase the a by 1 and then use the new value of a in the expression.a++ use the current value in the expression and then increase the a by 1
Did you read any of the posts in this thread above before posting?
//Btw a similar issue came up on clc++m recently - http://groups.google.co.uk/group/com...b9275faa0ec230
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 14th, 2007, 06:19 AM
#14
Re: A question for C++ expert on ++a and a++
 Originally Posted by exterminator
No, this cannot be the reason as any overload would be a function call and function call is a sequence point. The result would be unspecified but not undefined.
If IS correct because an overload IS a function call. Both side would be evaluated. However the built in's are NOT implemented as function calls. Therefore you can do
Code:
if ((ptr) && (ptr->MethodIO))
{
}
If the ptr is NULL, then the evauation stops, and there is no (illegal) attempt to call Method1 on the (null) pointer!
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 14th, 2007, 08:36 AM
#15
Re: A question for C++ expert on ++a and a++
I never thought of that before, but it does make sense that overloaded && and || cannot be 'lazy'.
That is indeed a good reason not to overload them, though I've never seen any point overloading them anyway.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|