分段错误,使用指向指针的指针

发布于 2024-08-10 04:36:47 字数 910 浏览 0 评论 0原文

所以我开始我的幻方硬件,我要求用户输入一个奇数整数,它将创建一个幻方。我必须使用指针和数组,因为这是我到目前为止所学到的全部内容。不是问如何做幻方,而是问是什么导致了分段错误,我可能没有正确执行指向二维数组的指针

#include <iostream>
using namespace std;

int main()
{
int **ptr;
int odd;
do 
{
    cout << "Enter a odd number to create a magic square: ";
    cin >> odd;
}while(odd % 2 != 1);

ptr = new int *[odd]; //creates a new array of pointers to int objects
for(int i = 0; i < odd; i++)
    ptr[i] = new int[odd];

//set it all to 0
for(int i = 0; i < odd; i++)
{
    for (int j = 0; j < odd; j++)
    {   
        ptr[i][j] = 0;
        cout << ptr[i][j];
    }
}
int row = odd;
int column = odd / 2;
int lastrow = row;
int lastcolumn = column;

//begin adding numbers to magic square
ptr[row][column] = 1;
for (int i = 2; i < odd * odd; i++)
{

}

//delete
for(int i = 0 ; i < odd; i++)
    delete [] ptr[i];
delete [] ptr;
return 0;
}

So I'm starting my magic square hw, where I ask the user to enter a odd integer, and it will create a magic square off that. I have to use pointers and arrays as that is all I have learned so far. Not asking how to do the magic square, but what is causing a segmentation fault, im probably not doing pointers to 2d arrays correctly

#include <iostream>
using namespace std;

int main()
{
int **ptr;
int odd;
do 
{
    cout << "Enter a odd number to create a magic square: ";
    cin >> odd;
}while(odd % 2 != 1);

ptr = new int *[odd]; //creates a new array of pointers to int objects
for(int i = 0; i < odd; i++)
    ptr[i] = new int[odd];

//set it all to 0
for(int i = 0; i < odd; i++)
{
    for (int j = 0; j < odd; j++)
    {   
        ptr[i][j] = 0;
        cout << ptr[i][j];
    }
}
int row = odd;
int column = odd / 2;
int lastrow = row;
int lastcolumn = column;

//begin adding numbers to magic square
ptr[row][column] = 1;
for (int i = 2; i < odd * odd; i++)
{

}

//delete
for(int i = 0 ; i < odd; i++)
    delete [] ptr[i];
delete [] ptr;
return 0;
}

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

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

发布评论

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

评论(4

踏雪无痕 2024-08-17 04:36:47

int row=odd; 应为 int row=odd-1;
稍后您将使用 row 进行索引,因此您会到达 array[size_of_array],它始终超出范围。

int row=odd; should be int row=odd-1;
You are later indexing with row, so you get to array[size_of_array], which is always out of bounds.

像你 2024-08-17 04:36:47

当您遇到分段错误时,要做的第一件事就是使用调试器并准确跟踪分段错误发生的位置,或者(更简单)在代码中的 cerr 中添加大量打印输出,这样您就可以看到有多少您的代码在段错误之前执行。

附加提示:使用 cerr 而不是 cout,以避免缓冲,否则可能会使您看不到在错误之前应该得到的一些输出,并且始终在每个打印输出中添加换行符,以避免它被 shell 中的下一个提示所破坏,其中你运行你的程序。

The first thing to do when you get a segmentation fault is either to grab a debugger and track down exactly where the segfault happens, or (simpler), to add a lot of printouts to cerr in the code, so you can see how much of your code gets executed before the segfault.

Additional hint: Use cerr rather than cout, to avoid buffering which might otherwise keep you from seeing some output which you should get before the error, and always add a newline to each printout to avoid having it clobbered by the next prompt in the shell where you run your program.

酒几许 2024-08-17 04:36:47

gdb 能够准确地告诉您段错误何时发生,并让您打印其中的数据。它可能是找出段错误发生位置的最佳选择。

使用 -g 编译你的程序(假设你使用 GCC)

gdb is able to tell you exactly when the segfault happens and lets you print the data from it. Its probably your best option for figuring out where the segfault happens.

Compile your program with -g (Assuming your using GCC)

醉梦枕江山 2024-08-17 04:36:47

这:

ptr[row][column] = 1;

访问数组越界(提示:在大多数 malloc 实现中,ptr[row] 可能为 NULL)。

This:

ptr[row][column] = 1;

accesses array out of bounds (hint: ptr[row] will likely be NULL in most malloc implementations).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文