++ A + ++ A + ++ A operation problem

xiaoxiao2021-03-05  36

Recently, I saw a discussion of A A, after I came back, I practiced in the dormitory. As the saying goes, the practice is the only standard for inspection truth! The analysis is not laughing in the master.

Problem program: Debug analysis.

Implementation under VC6.0

#include using namespace std; void main () {int a = 1; int b = A a; cout << b << endl;

}

I first saw this problem, I feel that I should be 9, but I'm trying to try the answer to the DEBUG mode under VC6.0. The answer to the Release mode is also 12, which is very wonderful.

So F11 but step track, look at the critical code segment. Still comparison is more clear.

0040157F MOV EAX, DWORD PTR [EBP-4] 00401582 Add Eax, 100401585 MOV DWORD PTR [EBP-4], EAX00401588 MOV ECX, DWORD PTR [EBP-4] 0040158B Add ECX, 10040158E MOV DWORD PTR [EBP-4], ECX00401591 MOV EDX, DWORD PTR [EBP-4] 00401594 Add Edx, DWORD PTR [EBP-4] 00401597 MOV EAX, DWORD PTR [EBP-4] 0040159A Add Eax, 10040159D MOV DWORD PTR [EBP-4], EAX004015A0 Add EDX DWORD PTR [EBP-4] 004015A3 MOV DWORD PTR [EBP-8], EDX

From this code, you can see that the execution process of A A A is like this:

First A, then A = 2 followed by the second A, then A = 3

Then in the calculation ( a) ( a)

Since this is the content of the A variable has changed to 3, A, i.e., 3 3 = 6, not 3 2 = 5 when the above code is used.

After that, the first A is calculated, then A = 4

So the final calculation result is b = 10.

As for the release mode, why is 12 I think it may be a compiler, because I don't know how to view the assembly code under Release,

So I don't know the true meaning now. Hope to enlighten me.

转载请注明原文地址:https://www.9cbs.com/read-33742.html

New Post(0)