倒带会导致分段错误

倒带会导致分段错误

本文介绍了倒带会导致分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在创建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 {
area [ i] ='\0';
}

for(int i = 0; i {
publisher [i] ='\0';
}

for(int i = 0; i {
title [i] ='\0';
}

for(int i = 0; i {
ISBNstr [i] ='\0';
}
registered = false;
}

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

  for(int i = 0; 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.

这篇关于倒带会导致分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:20