gpt4 book ai didi

c++ - 在 Visual Studio 中调试 C++ 代码指针时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:52:22 26 4
gpt4 key购买 nike

我想列出数组的每个成员及其相应的寄存器地址位置。这是我的代码

    // 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;

}

以 64 位运行得到以下意外结果

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

我不知道我做错了什么。第 4 列 (py) 开始列出数组中的第 4 项。但代码设置为读取第一个 (number[0])。我玩W10 Visual Studio Community 2017编译64位

更新 1:回应 PhoenixBlue 评论。修改数组示例以使用不同的数字。

更新 2:这是我的预期结果

   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

最佳答案

这段代码有问题py++ << ", " << *(py++) << ", " << *(++py) .

在 C++ 中,您不应在同一表达式中使用同一变量的多个增量。

像这样重写你的代码

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

这应该可以解决一些问题,尽管正如 Msalters 所说您还有其他问题。

关于c++ - 在 Visual Studio 中调试 C++ 代码指针时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51858417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com