FIX: Debugger Expands Arrays Passed to Functions IncorrectlyLast reviewed: September 18, 1997Article ID: Q112984 |
4.00 4.01 4.10 | 1.00 1.50 1.51 4.00 4.10
MS-DOS | WINDOWSkbtool kbfixlist kbbuglist The information in this article applies to:
SYMPTOMSIn C++ source files, passing an array as an argument to a function and then trying to watch that array in the function causes CodeView and the Visual Workbench Debugger to expand the array incorrectly.
RESOLUTIONDeclare the function argument as a pointer rather than an array. For example, change a declaration that uses the array syntax
void Function( int Array[5] )to a declaration that uses pointer syntax:
void Function( int * Array )This is not a problem in the 32-bit debuggers.
STATUSMicrosoft has confirmed this to be a problem in CodeView for MS-DOS, versions 4.0, 4.01, and 4.1, CodeView for Windows, versions 4.0 and 4.1, and the Visual Workbench Debugger, versions 1.0 and 1.5. This problem was corrected in the Visual Workbench Debugger included with Visual C++ for Windows version 1.52. This is still a problem in CodeView.
MORE INFORMATIONThe problem only occurs when compiling a .CPP file. Even though the array argument is not expanded correctly when debugging, the actual code generated by the compiler is correct and executes as expected. The sample code shown below illustrates this. Debugging the sample code in CodeView or the Visual Workbench Debugger and placing a watch on the variable named Array after tracing into Function() causes the Array argument to be expanded as:
Array[0] = 3052 Array[1] = 3052 Array[2] = 822 Array[3] = 4 Array[4] = 3The values for Array[0], Array[1], and Array[2] may be different than those shown above but they will be incorrect. Looking at the address of the Array argument shows that the debugger is looking at the wrong location on the stack. Even though the array is not expanded correctly, the output generated by the sample code is correct.
Sample Code
/* Compile options needed: /Zi */ #include <stdio.h> void Function( int Array[5] ){ int i; printf("\nInside Function: \n"); for( i = 0; i < 5; i++ ) printf("Array[%d] = %d\n", i, Array[i]); for( i = 0; i < 5; i++ ) Array[i] = i; } void main( void ){ int Array[5] = { 4, 3, 2, 1, 0 }; int i; printf("Before calling Function: \n"); for( i = 0; i < 5; i++ ) printf("Array[%d] = %d\n", i, Array[i]); Function( Array ); printf("\nAfter calling Function: \n"); for( i = 0; i < 5; i++ ) printf("Array[%d] = %d\n", i, Array[i]); } |
Additional reference words: 1.00 1.50 4.00 4.10 quickwatch locals
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |