Easily understand the essential difference between ++i and i++ from a memory perspective (you can understand it at a glance)

Easily understand the essential difference between ++i and i++ from a memory perspective (you can understand it at a glance)

In this article, I will mainly introduce some of the underlying logic differences between ++i and i++. I believe you have also seen a lot of information on the Internet that states: ++i first increments itself by +1 and then uses it (that is, then performs an expression operation); while i++ is used first and then increments by +1.

Next, we look at this process from a memory perspective through the following code and the assembly of the code.
You don’t need to know much assembly code to understand this.

int main()
{
    
    
    int b = 0;
    int a = 0;
    b = a++ //此时为后置++,即先使用,之后再进行++
   	return 0;
}

The following is the assembly code obtained by disassembly of this code in VS2019. The
Insert image description here
key analysis starts from b = a++;
The first mov instruction: represents that the variable a is first assigned to the eax register (the meaning of the register can be regarded as a temporary The role of storage);
the second mov instruction: then the value of the eax register is assigned to the variable b , so b gets this value.
The third mov instruction: at this time the value of the variable a is assigned to the ecx register (note the difference with eax is different above)
The fourth step is the add instruction: it means to +1 the value of ecx . The last step: the move instruction is to return the value after +1 to the variable a , so the value of a is 1 more at this time.

At this point we change a++ into ++a and look at the process again

int main()
{
    
    
	int b = 0;
	int a = 0;
	b = ++a;//前置++,先+1,再使用
	return 0;
}

Insert image description here
Let’s start with b= ++a;.
The first step is the mov instruction: assign the value of the variable a to the eax register.
The second step: the add instruction: add the value of eax + 1 (this step is also the key to distinguishing it from the above).
The third step mov instruction: The value of the eax register is returned to a, which is already the value after +1. The next
two steps are similar to the above. After the value of a is given to ecx, ecx assigns it to variable b.

From the above analysis, we can quickly understand why there is a +1 time difference between ++i and i++.

This is not over yet. Sometimes we have some concerns about ++i or i++ that appear alone. Here we will also conduct a wave of analysis.

int i = 0;
while (i < 32)
{
    
    
	printf("%d", i);
	i++; //此处也可以改成i++
}

If ++ or – appears alone like this, the essence is the same.
The old rule is to look at the assembly code!
Insert image description here
Here we only focus on the i++ step.
The first step: mov is to give the value of i to the register first.
The second step: just +1 directly (and in our above code, it has been used before, so the value will be returned to the user and then Perform the ++ operation, but there is no such operation here)
The third step is to return the value after adding 1 to the variable i

Next we change it to ++i;

Insert image description here
First of all, the first step is to give i to eax, the second step is to directly +1. The third step returns the value to i.

Summary : So when ++i or i++ appears alone (without a user), they actually mean the same thing.

PS : The last thing I want to tell you is that when you see these assembly codes, you may have this question in your mind, why do you need to use a register like eax every time?
In fact, the reason is very simple. The value of the variable is essentially in the memory , but once these values ​​are calculated, they will be loaded into the CPU . The calculation will only be performed in the CPU . They are first stored in the registers of the CPU and then used by the CPU. arithmetic and logical operations

It’s not easy to create. If you like it, give it a thumbs up! ! ! If you have any questions, feel free to communicate and see you in the comment area!

Guess you like

Origin blog.csdn.net/kklovecode/article/details/131545282