473,511 Members | 16,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple return question

int foo(int a)
{
return a++ * ++a;
}

On both Msvc++ and gcc I'm getting f(5) = 36. Shouldn't it be 30?

Mar 9 '06 #1
6 1850
* al*****@gmail.com:
int foo(int a)
{
return a++ * ++a;
}

On both Msvc++ and gcc I'm getting f(5) = 36. Shouldn't it be 30?


It's always a good idea to check the FAQ before posting.

This is a FAQ.

<url:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 9 '06 #2
In article <1141937801.289008.72480
@i39g2000cwa.googlegroups.com>, al*****@gmail.com says...
int foo(int a)
{
return a++ * ++a;
}

On both Msvc++ and gcc I'm getting f(5) = 36. Shouldn't it be 30?


It modifies a twice between sequence points, so it's
undefined behavior -- it could be 30 or 36, or it could
(in the immortal words) make demons fly out of your nose.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 9 '06 #3
al*****@gmail.com wrote:
int foo(int a)
{
return a++ * ++a;
}

On both Msvc++ and gcc I'm getting f(5) = 36. Shouldn't it be 30?


UB.
Why would one write such code?
Mar 9 '06 #4
the code return a++ * ++a; In Mvc debug window->disassembly
00401268 mov eax,dword ptr [ebp+8]
0040126B add eax,1
0040126E mov dword ptr [ebp+8],eax
00401271 mov eax,dword ptr [ebp+8]
00401274 imul eax,dword ptr [ebp+8]
00401278 mov ecx,dword ptr [ebp+8]
0040127B add ecx,1
0040127E mov dword ptr [ebp+8],ecx

it show us ,the Mvc just add once before multiply ,and after pop stack,
inc the ecx~
and if we modify the code as
mode 1:
int foo(int a)
{
int &k = a;
int &j = a;
k++;
++j;
return k*j;
} it will return the value 49.
the code mode 2:
int foo(int a)
{
int k = a;
int j = a;
k++;
++j;
return k*j;
}also return the value 36.so, i think the Mvc maybe use the variable in
code return (a++)*(++a) in mode 2

Mar 10 '06 #5

"Hippo" <hi*******@hotmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
| the code return a++ * ++a; In Mvc debug window->disassembly
| 00401268 mov eax,dword ptr [ebp+8]
| 0040126B add eax,1
| 0040126E mov dword ptr [ebp+8],eax
| 00401271 mov eax,dword ptr [ebp+8]
| 00401274 imul eax,dword ptr [ebp+8]
| 00401278 mov ecx,dword ptr [ebp+8]
| 0040127B add ecx,1
| 0040127E mov dword ptr [ebp+8],ecx
|
| it show us ,the Mvc just add once before multiply ,and after pop stack,
| inc the ecx~
| and if we modify the code as
| mode 1:
| int foo(int a)
| {
| int &k = a;
| int &j = a;
| k++;
| ++j;
| return k*j;
| } it will return the value 49.
| the code mode 2:
| int foo(int a)
| {
| int k = a;
| int j = a;
| k++;
| ++j;
| return k*j;
| }also return the value 36.so, i think the Mvc maybe use the variable in
| code return (a++)*(++a) in mode 2
|

With respect, it doesn't matter what one compiler does in this mode or that
mode. C++ is a language, when implemented with a few basic rules, will
generate an expected result from a valid sequence of statements, regardless
of the compiler, switch or platform.

The moment you start discussing what a piece of code does depending on what
a compiler's switch is set to: you've entered the never, never land of
undefined behaviour.

A programmer can ill afford playing games with his or her code. For all you
know, a service pack (or a client's specific compiler or compiler version)
can, and often does, modify the result. The rules in C++ are specifically
written to free a coder from such issues.

So basicly, i don't care what
return a++ * ++a;
actually does. It's undefined.

Mar 10 '06 #6
Peter_Julian wrote:
A programmer can ill afford playing games with his or her code. For all you
know, a service pack (or a client's specific compiler or compiler version)
can, and often does, modify the result. The rules in C++ are specifically
written to free a coder from such issues.


It may even be different next time the code is compiled, unless of
course this is documented by the compiler.

Still, it's a pointless discussion, introduce the sequence points and
ensure the behaviour is correct in every compiler today and tomorrow.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 10 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
2331
by: LRW | last post by:
Having a problem getting a onSubmit function to work, to where it popsup a confirmation depending on which radiobutton is selected. Here's what I have: function checkdel() { if...
6
3752
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
2
8374
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
73
4537
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
27
1822
by: karan.shashi | last post by:
Hey all, I was asked this question in an interview recently: Suppose you have the method signature bool MyPairSum(int array, int sum) the array has all unique values (no repeats), your...
14
2961
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
30
3472
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
1
2205
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post by:
Hi, A while ago I asked a question on the list about a simple eval function, capable of eval'ing simple python constructs (tuples, dicts, lists, strings, numbers etc) in a secure manner:...
10
2106
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
17
5788
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.