-1

您好,我在删除我的类的成员字符串时遇到了一些麻烦,该字符串不为空并且指向正确的位置,因为我可以在之前显示我的字符串“hello world”。

之后我从另一个成员函数调用函数 mystringclass::alloc() , this->str 应该让另一个字符串内容更大。

当我以相同的方式调整“hello”以获取“hello world”时,该过程第一次运行良好。但现在我想再次放大它并没有。所以我很困惑。

请帮我。

void mystringclass::alloc(long newsize) //newsize includes the +1 char
{   
   cout << "old size was: " << this->size << endl; //displays "12"
   if(this->str) cout << this->str << endl; //displays "hello world" all is right till here

   if(this->str) delete [] this->str ; //it crashes here

   cout << "str deleted\n"; //never show up on screen

   this->str = new char[newsize + 1];
   this->size = newsize;
   this->str[0] = 0;
}

感谢您的回答,我试图清除我的代码以将其发布在这里。我的错误消失了,但又出现了一个与三规则有关的错误:

int main()
{
      stringclass str = "Hello";
      stringclass str2 = str;
      return 0;
}

我在整个过程中都显示信息所以问题是 str2 已经等于“你好”,甚至在我的复制构造函数中影响 str 的内容之前。并且复制后为空。怎么了?有人可能会说,我是在魔法盒中学习 c++ 的。我正在使用代码块 10.05

完整代码:

#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

class stringclass
{
   protected :

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

   public :

      bool failbit;

      char * mystring;
      long memsize;
      long length;

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

      void copy(const stringclass & other);

      stringclass()                          {reset(); }
      stringclass(const stringclass & other) {copy(other); }
      stringclass(const char str[]);
      ~stringclass()                         {delete [] mystring;}

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

      friend ostream& operator << (ostream& out, stringclass & sc){out << sc.mystring; return out;}
      stringclass & operator = (stringclass & other)  { copy(other); return *this;}

};

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

void stringclass::alloc(long newsize)
{
   cout<< "\nalloc(long newsize)... \n" << endl;
   cout << "memsize was : " << memsize << endl;
   cout << "length was : " << length << endl;
   if(mystring) cout << "mystring = " << mystring << endl;

   delete [] mystring;

   cout << "mystring deleted...\n";

   mystring = new char[newsize];

   cout << "mystring has been resized\n";

   mystring[0] = 0;
   memsize = newsize;
   length = strlen(mystring);

   cout << "memsize is now : " << memsize << endl;
   cout << "length is now : " << length << endl;
   cout<< "\nend of alloc()... " << endl;
   cout << "\n";
}

void stringclass::copy(const stringclass & other)
{
   cout << "\n";
   cout << "copy(const stringclass & other)...\n" << endl;
   cout << "other.mystring = "<< other.mystring << endl;

   if(other.mystring == NULL || other.memsize == 0)
   {
      reset();
      return;
   }

   alloc(other.memsize);
   strcpy(mystring, other.mystring);

   cout << "mystring = "<< mystring;

   length = strlen(mystring);

   cout << "length: " << length << endl;
   cout<< "\nend of copy()... " << endl;
   cout << "\n";
}

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

int main()
{
      stringclass str = "Hello";
      stringclass str2 = str;

      cout << "\nback to main()...\n";
      cout << "str = " << str << "\n";
      cout << "str2 = " << str2 << "\n";
      cout << endl;

      system("PAUSE");
      return 0;
}

屏幕上的结果:

alloc(long newsize)...

memsize was : 0
length was : 0
mystring deleted...
mystring has been resized
memsize is now : 6
length is now : 0

end of alloc()...


copy(const stringclass & other)...

other.mystring = Hello

alloc(long newsize)...

memsize was : 3214960
length was : 2293560
mystring = Hello
mystring deleted...
mystring has been resized
memsize is now : 6
length is now : 0

end of alloc()...

mystring = length: 0

end of copy()...


back to main()...
str =
str2 =

Appuyez sur une touche pour continuer...

我刚刚意识到您不需要以下代码:

 protected :

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

       public :

          bool failbit;

所以我推迟了这两个函数和这个变量,猜猜是什么......一切都很好,没有错误。它们甚至一次都没有使用过。我把它放回去了,问题也回来了。你怎么解释?!我已经脱发了。

4

1 回答 1

0

不幸的是,您可以“显示”字符串这一事实完全没有说明。取消分配字符串仍将使其可打印,很可能直到它被新的定位和初始化覆盖。

只需在您完成后尝试打印字符串delete[]并查看。

于 2013-01-06T12:17:11.747 回答