simple return question

A

al.cpwn

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

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

Jerry Coffin

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.
 
H

Hippo

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
 
P

Peter_Julian

| 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.
 
B

Ben Pope

Peter_Julian said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top