This is the 25th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”


The problem code of the last article, I think it is necessary to separate out the talk! From a code perspective, take you hand to hand debugging (top) – Nuggets (juejin. Cn)

Review above:

A bad code

int main(a)
{
	int i = 0;
	int arr[10] = { 1.2.3.4.5.6.7.8.9.10 };
	for (i = 0; i <=12; i++)
	{
		arr[i] = 0;
		printf("hehe\n");
	}
	return 0;
}
Copy the code

Result: HeHE is printed in an infinite loop


To find problems: Print how many times to print

int main(a)
{
	int i = 0;
	int arr[10] = { 1.2.3.4.5.6.7.8.9.10 };
	for (i = 0; i <=12; i++)
	{
		arr[i] = 0;
		printf("hehe\n");
		printf("Print %d \n",i);
	}
	return 0;
}
Copy the code

I can’t go to 12


Debugging result:

When you change the subscript 12 of the first element to 0, I returns to 0, so an infinite loop is created

** Why?

Guess they take up the same space

– > validation



Deeply understand the err reasons of the above procedures

The stack area uses the high address first, then the low address. I define the variable I, so I is at the high address, and then I define the array, which is several addresses apart

The number of elements between arR and I is unknown, depending on the compiler


Variables and arrays are separated by distance

VC6. 0VS2013-VS2019 arr and I have two elements in the middle GCC: arr and I have one element in the middleCopy the code

Therefore, under VC6.0, arr[10], written as I <= 10, will be in an infinite loop

How to explain:

I and ARR are local variables and are placed in the stack area. The usage of the stack area is to use the high address first, then use the low address to draw the stack area memory, the low address above, the high address below, first use the high address space I created at the bottom, arR created at the top, and because as the array subscript grows, The address of the element is low to high and as the index of the array is accessed out of bounds, it can overwrite the loop variable I (both take up the same space)Copy the code

Q: When the creation of I and ARR is reversed: an error is reported without an infinite loop

Because the memory layout has changed

int main(a)
{
	int arr[10] = { 1.2.3.4.5.6.7.8.9.10 };
	int i = 0;
	for (i = 0; i <=12; i++)
	{
		arr[i] = 0;
		printf("hehe\n");
		printf("Print %d \n",i);
	}
	return 0;
}
Copy the code

Execution result: An error is reported

Pay attention to

Program out of bounds has an error point, the program stopped to report an error, there is no time to report an error loop


If changed to release version:

Result: Thirteen HEHe packets are printed


In Debug: I is actually stored after the array, the address of I is larger than the address of the array


In the Release version: the address of the array is larger and the compiler optimizes it to allow for errors after the array


This also proves that the Release version can be optimized


That’s all for today. Thank you for seeing us! Hope to help you! You are welcome to click on this topic and subscribe! At the same time, welcome the bigwigs to criticize and correct!