指向指针数组的分段错误和内存损坏

发布于 2024-12-04 13:38:14 字数 608 浏览 2 评论 0原文

这里的目标是拥有一个像二维矩阵一样工作的指针。我已经测试了以下代码,用于创建指向整数指针的指针和整数指针数组。它编译并运行良好。 (这是一个简化版本,我尝试将值分配/打印到 mymatrix[x][y] 并且工作正常。)

#include <iostream>
int **mymatrix;

int main(int argc, char* args[]){

  mymatrix = new  int*[100] ;
  for( int n = 0; n <= 100; n++ ){
    mymatrix[n] = new int[200] ;
  }
  return 0;
}

但是,一旦我将此代码段复制到另一个代码(其中之前运行良好),代码仍然可以编译但无法运行。没有与此代码片段相关的错误或警告。这很奇怪,因为 mymatrix 甚至还没有与其余代码交互(定义后就再也没有使用过)。

中断执行的实际错误在尝试加载字体时的崩溃、尝试分配值时的分段错误和内存损坏(具有大量日志输出我认为是线程)。

我可以粘贴较大代码的部分,但我几乎确信这一切都发生了,因为我没有以正确的方式定义这个指针。我在这里做了一些笨拙或不安全的事情吗?或者我应该开始检查其余代码是否有错误?

The objective here is to have a pointer that works like a 2D matrix. I've tested the following bit of code for creating a pointer to and array of integer pointers. It compiles and runs fine. (This is a simplified version, I've tried assigning/printing values to mymatrix[x][y] and it works fine.)

#include <iostream>
int **mymatrix;

int main(int argc, char* args[]){

  mymatrix = new  int*[100] ;
  for( int n = 0; n <= 100; n++ ){
    mymatrix[n] = new int[200] ;
  }
  return 0;
}

However, as soon as I copy this snippet to another code (which previously ran fine), the code still compiles but fails to run. There are no errors or warnings related to this snippet. This is weird because mymatrix doesn't even interact with the rest of the code yet (after being defined it's just never used again).

The actual error which interrupts the execution varies between crashing when trying to load a font, Segmentation Fault when trying to assign a value, and Memory Corruption (with a huge log output of what I think are threads).

I could paste sections of the larger code, but I'm almost convinced that this is all happening because I'm not defining this pointer in the proper way. Am I doing something clumsy or unsafe here? Or should I start looking through the rest of my code for bugs?

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

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

发布评论

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

评论(1

深海里的那抹蓝 2024-12-11 13:38:14
for( int n = 0; n <= 100; n++ )

这会导致数组溢出。 mymatrix[100] 不是 mymatrix 数组的一部分。写入它的结果是未定义的。它本身“工作正常”的原因是没有其他数据结构可供您破坏。 for 循环应该以.

for( int n = 0; n < 100; n++ )

一般来说,在 for 循环条件中使用 <= 不是惯用的 c.如果您发现自己在使用它,您应该问问自己是否真的在做正确的事情。数组溢出有时很难发现。如果您使用的是 Linux,则可以使用 valgrind 之类的工具来帮助您识别此错误和其他错误。

for( int n = 0; n <= 100; n++ )

This causes an array overrun. mymatrix[100] is not part of the mymatrix array. The result of writing to it is undefined. The reason it "works fine" by itself is that there aren't other data structures for you to clobber. The for loop should start with.

for( int n = 0; n < 100; n++ )

In general the use of <= in for loop conditions is not idiomatic c. If you find yourself using it, you should ask yourself if you are really doing the right thing. Array overruns are sometimes hard to find. If you are on linux, you can use a tool like valgrind to help you identify this and other errors.

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