FIX: Debugger Expands Arrays Passed to Functions Incorrectly

ID: Q112984

4.00 4.01 4.10 | 1.00 1.50 1.51 4.00 4.10

MS-DOS         | WINDOWS
kbtool kbfixlist kbbuglist

The information in this article applies to:

SYMPTOMS

In 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.

RESOLUTION

Declare 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.

STATUS

Microsoft 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 INFORMATION

The 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] = 3

The 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 KBCategory: kbtool kbfixlist kbbuglist KBSubcategory: WBDebug CvwIss
Keywords          : kb16bitonly kbCodeView kbDebug kbide kbVC kbbuglist kbfixlist
Version           : 4.00 4.01 4.10 | 1.00 1.50 1.51
Platform          : MS-DOS WINDOWS
Solution Type     : kbfix

Last Reviewed: September 22, 1997