倒带会导致分段错误

编程入门 行业动态 更新时间:2024-10-23 22:27:41
本文介绍了倒带会导致分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我的程序出现分段错误时,我一直在尝试进行一些调试。

在创建ISBNPrefix对象时打开文件

ISBNPrefix :: ISBNPrefix(const char * filename) { file = fopen(filename,r); }

文件是ISBNPrefix类的成员:

class ISBNPrefix { FILE * file; public: ISBNPrefix(const char * filename); bool isRegistered(int area)const; int minNoDigits(int area)const; bool isRegistered(int area,const char * publisher)const; 〜ISBNPrefix(); };

创建ISBNPrefix对象的行是:

ISBNPrefix prefixList(prefixRanges.txt);

prefixRanges.txt是我目录中文件的名称

现在我在此部分发生分段错误:

// ------ ------------------------- cout<< MADE IT 1< endl; // ------------------------------- rewind(file); // ------------------------------- cout<< MADE IT 2< endl; // -------------------------------

哪些输出:

MADE IT 1 分段错误

解构函数:

ISBNPrefix ::〜ISBNPrefix() { if(file!= NULL) { fclose(file); } }

是文件关闭的唯一位置

编辑:经过一些故障排除,找不到解决方案,建议我张贴一切。由于我的声誉低,我不能发布超过2个链接,pastebin链接在评论部分。

解决方案

我没有看到你看到的问题,但我看到一个严重的问题。在我的系统上,它导致您的程序崩溃退出。

class ISBN { char area [5]; char publisher [7]; char title [6]; bool registered; char ISBNstr [11]; bool isRegistered(const ISBNPrefix& list);

和此代码在ISBN构造函数中

ISBN :: ISBN() { for(int i = 0; i <= 5; i ++) { area [ i] ='\0'; } for(int i = 0; i <= 7; i ++) { publisher [i] ='\0'; } for(int i = 0; i <= 6; i ++) { title [i] ='\0'; } for(int i = 0; i <= 11; i ++) { ISBNstr [i] ='\0'; } registered = false; }

这些循环遍历过多次。例如。应在区域循环中 i <5 不 i <= 5

for(int i = 0; i <5; i ++) { area [i] ='\0'; }

因为这个错误你正在破坏内存,正在查看。

I've been trying to do some debugging when my program came up with a segmentation fault. I've tracked it down to where rewind is called.

The file is opened when an ISBNPrefix object is created

ISBNPrefix::ISBNPrefix(const char* filename) { file = fopen( filename, "r" ); }

file is a member of the ISBNPrefix class:

class ISBNPrefix { FILE* file; public: ISBNPrefix(const char* filename); bool isRegistered(int area) const; int minNoDigits(int area) const; bool isRegistered(int area, const char* publisher) const; ~ISBNPrefix(); };

The line that creates the ISBNPrefix object is:

ISBNPrefix prefixList("prefixRanges.txt");

prefixRanges.txt is the name of the file in my directory

Right now I have the segmentation fault occuring at this section:

//------------------------------- cout << "MADE IT 1" << endl; //------------------------------- rewind( file ); //------------------------------- cout << "MADE IT 2" << endl; //-------------------------------

Which outputs:

MADE IT 1 Segmentation fault

The deconstructor:

ISBNPrefix::~ISBNPrefix() { if( file != NULL ) { fclose(file); } }

Is the only place that the file is closed

Edit: After some troubleshooting, a solution wasn't found and it was suggested that I post everything. Since my reputation is low and I can't post more than 2 links, the pastebin links are in the comments section.

解决方案

I don't see the problem you see, but I do see a serious problem. On my system it causes your program to crash on exit. Maybe on your system it's the cause of your problem.

This code in the ISBN header

class ISBN{ char area[5]; char publisher[7]; char title[6]; bool registered; char ISBNstr[11]; bool isRegistered(const ISBNPrefix& list);

and this code in the ISBN constructor

ISBN::ISBN() { for(int i=0;i<=5;i++) { area[i] = '\0'; } for(int i=0;i<=7;i++) { publisher[i] = '\0'; } for(int i=0;i<=6;i++) { title[i] = '\0'; } for(int i=0;i<=11;i++) { ISBNstr[i] = '\0'; } registered = false; }

Those loops all go round one too many times. E.g. it should be i<5 not i<=5 in the area loop

for(int i=0;i<5;i++) { area[i] = '\0'; }

Because of this bug you are corrupting memory and that could easily cause the problem you are seeing.

更多推荐

倒带会导致分段错误

本文发布于:2023-07-11 09:13:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1093543.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   倒带会

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!