Yesterday, I discussed with a classmate about debugging Arrays for dynamic allocation.
The most direct and original way to dynamically allocate arrays is to use a pointer to dynamically allocate memory space. The following code:
// Dynamically allocate a behavior 2 column as a 2-dimensional array with the Member values of 1, 2, 4, and 3
Int rows, cols;
// Set rows and columns to 2
Rows = Cols = 2;
// Array header pointer
Int **;
// Use malloc to allocate space
A = (INT **) malloc (rows * sizeof (int *));
If (A = NULL)
Return 1;
For (INT I = 0; I <rows; I ++)
{
A [I] = (int *) malloc (Cols * sizeof (INT ));
If (A [I] = NULL)
Return 1;
}
// Assign values to members
A [0] [0] = 1;
A [0] [1] = 2;
A [1] [0] = 3;
A [1] [1] = 4;
When debugging a program, because the target object A is not a static array with a length known by the compiler, but a series of pointers, all the members of the array cannot be displayed during debugging, instead of being a pointer, display the content it points to. Because a is a pointer to a pointer, the space that a points to is a pointer, visual Studio will display the number 1 finally pointed to by this pointer again. So a in the local data in the initial debugging window looks like this:
The solution is to add the variable to the listener window, and add a dynamic debugging expression to the listener window to preview the content in:
Then, in the listener window, change the original A to A, 2 (A comma 2), which tells Visual Studio, let's look at the content of the two elements after starting point a (you can also use the plus sign in debugging to modify the position of this starting point ):
Then press Enter:
At this point, the two pointers that a points to, that is, the header pointers of the two rows of the array (pointing to 1 and 3 respectively, which can be seen in the listener window), are displayed.
However, all the members of the entire array are not displayed yet. In this case, you need to add the two headers to listen again, select [0] and [1] above, right-click, add watch ):
Now the watch 1 window has three data items:
Since the number of columns in the array is 2, add Comma 2 after the two array row header pointers to view the values of the two elements pointed to by the array. Finally, the members of the entire array are displayed:
Here is another technique. when the first one adds a to the listening window, the interface is like this (in this case, the interface is switched from locals to watch 1 ):
Watch is different from locals in that we can modify the value of the expression. Of course, we can also add any number of expressions. In the blue selection box in the watch, there will be no more in locals. It is just to add a debugging expression.
In this case, we can also use an expression to listen to data in:
Enter * a, 2, and * (a + 1) in sequence. The result of 2 is displayed:
It can be seen that the dynamic allocation of arrays is not only inconvenient to write, but also inconvenient to debug (it will be inconvenient to maintain in the future ), we recommend that you use vector to create dynamic arrays and internal data structures in the C ++ standard library. Debugging is also very convenient:
The following Code declares the preceding two-dimensional array using vector:
// + # Include <vector>
// Dynamically allocate a behavior 2 column as the 2-dimensional vector <int> with the Member values 1, 2, 4, and 3
Int rows, cols;
// Set rows and columns to 2
Rows = Cols = 2;
Vector <vector <int>;
// Adjust the size
A. Resize (rows );
For (INT I = 0; I <rows; I ++)
A [I]. Resize (Cols );
// Assign values to members
A [0] [0] = 1;
A [0] [1] = 2;
A [1] [0] = 3;
A [1] [1] = 4;
Then it is very convenient to debug and you do not need to do anything else. The data is clear at a glance: