简体   繁体   中英

trouble debugging c++ code pointer in Visual studio

I will like to list each member of an array listed with its corresponding register address location. Here is my code

    // PointerDeferenceTest.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    int main()
    {
        int x=0, y=0;
        int *px, *py;

        int number[15] = {-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9};
        while (x<14)
        {
            px = &x;
            py = number+x;
            cout << x+1 << ", " << px << ", " << *px << ", " << py << ", " <<  *py << ", " << py++ << ", " << *(py++) << ", " << *(++py) << "  \n";
            ++x;
        }
        return 0;

}

Running at 64 bits getting the following unexpected result

1, 000000D7532FF874, 0, 000000D7532FF904, -1, 000000D7532FF900, -3, -1
2, 000000D7532FF874, 1, 000000D7532FF908, 0, 000000D7532FF904, -2, 0
3, 000000D7532FF874, 2, 000000D7532FF90C, 1, 000000D7532FF908, -1, 1
4, 000000D7532FF874, 3, 000000D7532FF910, 2, 000000D7532FF90C, 0, 2
5, 000000D7532FF874, 4, 000000D7532FF914, 3, 000000D7532FF910, 1, 3
6, 000000D7532FF874, 5, 000000D7532FF918, 4, 000000D7532FF914, 2, 4
7, 000000D7532FF874, 6, 000000D7532FF91C, 5, 000000D7532FF918, 3, 5
8, 000000D7532FF874, 7, 000000D7532FF920, 6, 000000D7532FF91C, 4, 6
9, 000000D7532FF874, 8, 000000D7532FF924, 7, 000000D7532FF920, 5, 7
10, 000000D7532FF874, 9, 000000D7532FF928, 8, 000000D7532FF924, 6, 8
11, 000000D7532FF874, 10, 000000D7532FF92C, 9, 000000D7532FF928, 7, 9
12, 000000D7532FF874, 11, 000000D7532FF930, 0, 000000D7532FF92C, 8, 0
13, 000000D7532FF874, 12, 000000D7532FF934, -858993460, 000000D7532FF930, 9, -858993460
14, 000000D7532FF874, 13, 000000D7532FF938, -858993460, 000000D7532FF934, 0, -858993460

I cannot figure out what I am doing wrong. The 4th column (py) is starting to list the 4th item in the array. But the code is set to read the first (number[0]). I amusing W10 Visual studio community 2017 compile 64 bit

UPDATE 1: In response to PhoenixBlue comment. Amended the array sample to use different numbers.

UPDATE 2: This is my expected result

   1, 000000D7532FF874, 0, 000000D7532FF904, -4, 000000D7532FF900, -3, -1
    2, 000000D7532FF874, 1, 000000D7532FF908, -3, 000000D7532FF904, -2, 0
    3, 000000D7532FF874, 2, 000000D7532FF90C, -2, 000000D7532FF908, -1, 1
    4, 000000D7532FF874, 3, 000000D7532FF910, -1, 000000D7532FF90C, 0, 2
    5, 000000D7532FF874, 4, 000000D7532FF914, 0, 000000D7532FF910, 1, 3
    6, 000000D7532FF874, 5, 000000D7532FF918, 1, 000000D7532FF914, 2, 4
    7, 000000D7532FF874, 6, 000000D7532FF91C, 2, 000000D7532FF918, 3, 5
    8, 000000D7532FF874, 7, 000000D7532FF920, 3, 000000D7532FF91C, 4, 6
    9, 000000D7532FF874, 8, 000000D7532FF924,4, 000000D7532FF920, 5, 7
    10, 000000D7532FF874, 9, 000000D7532FF928, 5, 000000D7532FF924, 6, 8
    11, 000000D7532FF874, 10, 000000D7532FF92C, 6, 000000D7532FF928, 7, 9
    12, 000000D7532FF874, 11, 000000D7532FF930, 7, 000000D7532FF92C, 8, 0
    13, 000000D7532FF874, 12, 000000D7532FF934, 8, 000000D7532FF930, 9, -858993460
    14, 000000D7532FF874, 13, 000000D7532FF938, 9, 000000D7532FF934, 0, -858993460

This code is a problem py++ << ", " << *(py++) << ", " << *(++py) .

In C++ you should not use multiple increments of the same variable in the same expression.

Rewrite your code like this

cout << x+1 << ", " << px << ", " << *px << ", " << py << ", " <<  *py << ", ";
cout << py++ << ", ";
cout << *(py++) << ", ";
cout << *(++py) << "  \n";

That should fix some of the problems although as Msalters says you have other problems as well.

py overflows, when x==13 , and so py=&number[13] before you increment it twice. That is Undefined Behavior, which may result in any outcome.

You probably will have more issues once you've fixed this, but Undefined Behavior is so severe that you cannot reason about minor bugs in the presence of UB. UB is so bad, it can appear to "travel back in time". You can't say "Foo happened before the UB, so it should be unaffected by it". Since UB can do anything, it can also change the past.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM