为什么我在 c++ 中通过 a[n][n] 声明表达式 a[1][1] 后无法观看它?

发布于 2024-11-08 00:18:35 字数 412 浏览 0 评论 0原文

我的代码:

#include <iostream>
using namespace std;
int main() {
    int n=5;
    int a[n][n];
    a[1][1]=5;
    return 0;
}

当我尝试在 eclipse 中第 6 行观察表达式 a[1][1] 时,出现此错误:

执行 MI 命令失败: -data-evaluate-expression a[1][1] 来自调试器后端的错误消息: 无法执行指针数学运算 不完整的类型,尝试转换为 已知类型,或 void *。

我猜它是从 gdb 返回的?但是,我不知道为什么我看不到这个值? “a”不是一个普通的多维数组吗?

my code:

#include <iostream>
using namespace std;
int main() {
    int n=5;
    int a[n][n];
    a[1][1]=5;
    return 0;
}

I got this error when trying to watch the expression a[1][1] in eclipse on line 6:

Failed to execute MI command:
-data-evaluate-expression a[1][1] Error message from debugger back end:
Cannot perform pointer math on
incomplete types, try casting to a
known type, or void *.

i guess it's returned from gdb? however, i don't know why i can't watch that value? Isn't "a" is a normal multi-dimensional array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨后彩虹 2024-11-15 00:18:35

由于一些奇怪的原因,这不是有效的 C++,除非您创建它

const int n = 5;

,否则数组大小在运行时之前是正式未知的。

For some odd reasons this isn't valid C++ unless you make it

const int n = 5;

Otherwise the array size is formally unknown until runtime.

落在眉间の轻吻 2024-11-15 00:18:35

C++ 不假定可变长度数组 (VLA)。所以你的代码不是标准一致的代码。

如果您使用 g++ -pedantic 编译它,它将无法编译。数组大小必须是常量表达式。但在你的代码中,它不是。

所以写:

 const int n=5; //now this becomes constant!
 int a[n][n]; //the size should be constant expression.

让我们尝试上面的代码,因为它现在完全符合标准代码。

C++ doesn't suppose variable length array (VLA). So your code is not standard conformant code.

It will not compile if you compile it with g++ -pedantic. The array size must be constant expression. But in your code, its not.

So write:

 const int n=5; //now this becomes constant!
 int a[n][n]; //the size should be constant expression.

Lets try the above code, as its completely Standard conformant code now.

十年九夏 2024-11-15 00:18:35

为什么不更好地做一个动态二维数组呢?在这种情况下,您不必使 n 恒定,并且可以动态确定大小。

int **arr, n;

arr = new int * [n]; // allocate the 1st dimension. each location will hole one array
for (i=0; i<n; i++)
{
  arr[i] = new int [n]; // allocate the 2nd dimension of one single n element array
                        // and assign it to the above allocated locations.
}

现在您可以通过 arr[i][j] 访问数组

以释放反向空间

for (i=0; i<n; i++)
{
  delete [] arr[i]; // first delete all the 2nd dimenstion (arr[i])
}
delete [] arr; // then delete the location arays which held the address of the above (arr)

why not better do it a dynamic 2d array? In that case you do not have to make the n constant, and you can determine the size dynamically.

int **arr, n;

arr = new int * [n]; // allocate the 1st dimension. each location will hole one array
for (i=0; i<n; i++)
{
  arr[i] = new int [n]; // allocate the 2nd dimension of one single n element array
                        // and assign it to the above allocated locations.
}

Now you can access the aray as arr[i][j]

To free to the reverse

for (i=0; i<n; i++)
{
  delete [] arr[i]; // first delete all the 2nd dimenstion (arr[i])
}
delete [] arr; // then delete the location arays which held the address of the above (arr)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文