给出不同输入时出现分段错误
我用 C++ 做了一些图像处理工作。为此,我使用 CImg.h 库,我觉得它对我的工作很有好处。
这是我编写的一小段代码,它只读取图像并显示它。
#include "../CImg.h"
#include "iostream"
using namespace std;
using namespace cimg_library;
int main(int argc,char**argv)
{
CImg<unsigned char> img(argv[1]);
img.display();
return 0;
}
当我将 lena.pgm 作为输入时,它会显示图像。就好像我给出了一些其他图像,例如 ddnl.pgm ,我出现在同一目录中,我得到“分段错误”。
当我使用 gdb 运行代码时,我得到的输出如下:
程序收到信号 SIGSEGV,分段错误。
/lib/libc.so.6 中的 strlen () 中的 0x009823a3
缺少单独的调试信息,请使用: debuginfo-install glibc-2.9-2.i686 libX11-1.1.4-5.fc10.i386 libXau-1.0.4-1.fc10.i386 libXdmcp-1.0.2-6.fc10.i386 libgcc -4.3.2-7.i386 libstdc++-4.3.2-7.i386 libxcb-1.1.91-5.fc10.i386
有人可以告诉我问题是什么吗?以及如何解决。
谢谢大家
I do some image processing work in C++. For this i use CImg.h library which i feel is good for my work.
Here is small piece of code written by me which just reads an image and displays it.
#include "../CImg.h"
#include "iostream"
using namespace std;
using namespace cimg_library;
int main(int argc,char**argv)
{
CImg<unsigned char> img(argv[1]);
img.display();
return 0;
}
When i give lena.pgm as input this code it displays the image. Where as if i give some other image, for example ddnl.pgm which i present in the same directory i get "Segmentation Fault".
When i ran the code using gdb i get the output as follows:
Program received signal SIGSEGV, Segmentation fault.
0x009823a3 in strlen () from /lib/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.9-2.i686 libX11-1.1.4-5.fc10.i386 libXau-1.0.4-1.fc10.i386 libXdmcp-1.0.2-6.fc10.i386 libgcc-4.3.2-7.i386 libstdc++-4.3.2-7.i386 libxcb-1.1.91-5.fc10.i386
Can some one please tell me what the problem is? and how to solve it.
Thank you all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您尝试访问不允许访问的内存时,就会出现段错误。
所以请在代码中检查一下。
Segfault comes when you are trying to access memrory which you are not allowed to access.
So please check that out in the code.
代码本身看起来很好。我可以建议一些继续调试的方法 -
一旦你掌握了更多信息,就可以说更多。
编辑 -
查看您提供的额外信息,并查阅代码本身,CImg 在尝试检查您正在打开的文件类型时似乎失败了。
相关的代码行是 -
这是第一次使用“ftype”,这使我得出的结论是它的值无效。
'ftype' 在上面几行被赋予了一个值 -
file_type() 函数本身尝试根据文件头猜测要打开的文件,可能是因为根据扩展名打开它失败了。只有一种明智的方法可以让它返回无效值,这会导致 strcmp() 失败 - 当它无法将文件识别为它熟悉的任何内容时,它会返回 NULL(实际上是 0)。
因此,我重申我的建议,即您尝试验证这确实是一个有效文件。我无法向您指出任何能够打开/保存 PGM 文件的工具,但我猜简单的 Google 搜索会有帮助。尝试打开该文件并将其重新另存为 PGM。
The code itself looks just fine. I can suggest some ways to go ahead with debugging -
Once you have more information, more can be said.
EDIT -
Looking at the extra information you provided, and consulting the code itself, it appears that CImg is failing when trying to check what kind of file you are opening.
The relevant line of code is -
This is the first time 'ftype' is used, which brings me to the conclusion that it has an invalid value.
'ftype' is being given a value just a few lines above -
The file_type() function itself tries to guess what file to open based on its header, probably because opening it based on the extension - failed. There is only one sane way for it to return an invalid value, which would later cause strcmp() to fail - when it fails to identify the file as anything it is familiar with, it returns NULL (0, actually).
So, I reiterate my suggestion that you try to verify that this is indeed a valid file. I can't point you at any tools that are capable of opening/saving PGM files, but I'm guessing a simple Google search would help. Try to open the file and re-save it as PGM.
分段错误的另一个“有趣的追踪”原因是库之间的编译器不匹配 - 这在使用 C++ 库时尤其普遍。
需要检查的事情是:
这些都曾以微妙的方式刺痛过我。
Another "fun to track down" cause of segmentation faults is compilier mismatches between libraries - this is especially prevalent when using C++ libraries.
Things to check are:
Each of these has bitten me in subtle ways before.