In "C Traps and defects", the following code appears:
#include <stdio.h>int main (int argc, char *argv[]) {int i = 0;int a[10];for (i=0; i<=10; ++i) {a[i] = 0;//Dead Loop}retu RN 0;}
The author's explanation is that if the compiler used to compile the program allocates memory to the variable in a way that decrements the memory address, the final assignment is to the variable I and into the dead loop.
Run the program in VC6.0 to see the disassembly code:
From the results, it is known that the allocation of memory results in decreasing the address of the variable to allocate memory:
Results, A[i] = 0 Disassembly code is mov dword ptr [EBP+ECX*4-2CH], 0
where ECX is the value of I, ebp-2ch = ebp-44, is actually a[0], I is 4 bytes, a[10] is 40 bytes, a total of 44 bytes.
mov dword ptr [EBP+ECX*4-2CH], 0 is also mov dword ptr [ebp-2ch+ecx*4], 0
Is the address of the a[0], plus the integer address of I units (4 bytes) each time
When the last value i = 10, it becomes the Mov dword ptr [ebp-4], 0, is actually to assign to I, this statement is the first breakpoint position disassembly code, natural, I was re-assigned to 0, into the dead loop.
This is the first time you have used disassembly to debug a program, simply record it.
Disassembly Debug Dead Loop