FIX: Debugger Expands Arrays Passed to Functions Incorrectly

Last reviewed: September 18, 1997
Article 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:

  • The Visual Workbench Integrated Debugger included with:

        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, and 1.51
    
  • Microsoft CodeView for MS-DOS, versions 4.0, 4.01, and 4.1
  • Microsoft CodeView for Windows, versions 4.0 and 4.1

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 : CvwIss kb16bitonly WBDebug kbbuglist kbfixlist kbtool
Version : 4.00 4.01 4.10 | 1.00 1.50 1.51
Platform : MS-DOS WINDOWS
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.