0

在修复了来自未初始化指针的错误之后。一个旧的错误仍然存​​在,stringclass::alloc()如果我删除 mystring 尽管我们可以在之前处理或显示它,它会崩溃。它已经初始化了,那么这次发生了什么?

void stringclass::alloc(long newsize)
{     
   if(memsize < newsize)
   {
       cout << "checkpoint 1\n";
       if(mystring) delete [] mystring;
       cout << "checkpoint 2\n";
       memsize = newsize;
       mystring = new char[memsize];
       mystring[0] = 0;
       length = 0;
    }
}

完整代码:

#include <iostream>
using namespace std;


#define DEFAULT_BREAKPOINT -1
#define DEFAULT_STARTPOINT -1

class stringclass
{
   protected :

      inline bool success()   { failbit = false;  return true; }
      inline bool fail()      { failbit = true; return false; }

   public :

      bool failbit;

      long memsize;
      long length;
      char * mystring;

      bool ins(const char * str, long startpoint, long breakpoint);


      inline long get_length()       { if(mystring) length = strlen(mystring); return length;}
      inline long get_memsize() const { return memsize; }

      void reset();
      void alloc(long newsize);

      void copy(const stringclass & other);
      stringclass();
      stringclass(const char str[]);
      stringclass(const stringclass & other);
      ~stringclass();

      friend ostream& operator << (ostream& out, stringclass & sc){out << sc.mystring; return out;}

};

void stringclass::copy(const stringclass & other)
{
   if(other.mystring == NULL)
   {
      reset();
      return;
   }

   alloc(other.memsize);
   strcpy(mystring, other.mystring);
   length = other.length;
}

stringclass::stringclass()
   : mystring(NULL), memsize(0), length(0)
{
}

stringclass::stringclass(const char str[])
   : mystring(NULL), memsize(0), length(0)
{
   if(str != NULL)
   {
      alloc(strlen(str) + 1);
      strcpy(mystring, str);
      length = strlen(mystring);
   }
}

stringclass::stringclass(const stringclass & other) 
   : mystring(NULL), memsize(0), length(0)
{
   copy(other);
}

stringclass::~stringclass()
{
   delete [] mystring;
}

void stringclass::reset()
{
   if(mystring) delete [] mystring;
   mystring = NULL;
   length = 0;
   memsize = 0;
}

void stringclass::alloc(long newsize)
{     
   if(memsize < newsize)
   {
       cout << "checkpoint 1\n";
       if(mystring) delete [] mystring;
       cout << "checkpoint 2\n";
       memsize = newsize;
       mystring = new char[memsize];
       mystring[0] = 0;
       length = 0;
    }
}

bool stringclass::ins(const char * str, long startpoint = DEFAULT_STARTPOINT, long breakpoint = DEFAULT_BREAKPOINT)
{
   if(startpoint == DEFAULT_STARTPOINT) startpoint = 0;
   if(breakpoint == DEFAULT_BREAKPOINT) breakpoint = startpoint;

   if(breakpoint > length || breakpoint - startpoint < 0) return fail();
   if(str == NULL) return fail();

   long str_length = strlen(str);
   long temp_size = 0;
   bool to_resize = false;

   if(length + str_length + 1 - (breakpoint - startpoint) > memsize)
   {
      temp_size = (length + str_length + 1 - (breakpoint - startpoint));
      to_resize = true;
   }
   else temp_size = memsize;

   char * temp = new char[temp_size];

   long temp_i = 0;

   if(mystring) for(; temp_i < startpoint; temp_i++) temp[temp_i] = mystring[temp_i];
   for(long str_i = 0; str_i < str_length; str_i++, temp_i++) temp[temp_i] = str[str_i];
   if(mystring) for(long buf_i = breakpoint; buf_i < memsize; buf_i++, temp_i++)temp[temp_i] = mystring[buf_i];

   temp[temp_i] = 0;

   if(to_resize) alloc(temp_size); //empty

   for(long buf_i = 0; buf_i < memsize; buf_i++) mystring[buf_i] = temp[buf_i];

   if(temp) delete [] temp;
   length = strlen(mystring);

   return success();
}


int main()
{

      stringclass str = "Hello";

      str.ins("rld", str.get_length());
      cout << str << endl;

      str.ins(" the wo", 5);
      cout << str << endl;

      system("PAUSE");
      return 0;
}
4

1 回答 1

2

你溢出了一个缓冲区。

这个

char * temp = new char[temp_size];

应该

char * temp = new char[temp_size+1];

此外,您的班级没有赋值运算符,因此分配它会进行浅拷贝并导致双重删除。

于 2013-01-06T20:01:35.553 回答