-1

こんにちは、直前に文字列「hello world」を表示できるため、null ではなく、正しい場所を指すクラスのメンバー文字列を削除するのに問題があります。

関数 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;
}

ご回答ありがとうございます。ここに投稿するためにコードをクリアしようとしました。私のバグは消えましたが、別のバグが発生し、3 のルールと関係があります。

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

私はすべての手順に沿って情報を表示します。したがって、問題は、コピー コンストラクターの str の内容に影響を与える前であっても、str2 が既に "hello" に等しいことです。コピー後は空です。どうしたの?言う人もいるかもしれませんが、私はマジックボックスで 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;

だから私はこれらの2つの関数とこの変数を延期し、何を推測します..すべてうまくいきました.バグはありません. それらは一度も使用されていません。私はそれを元に戻しましたが、問題も再発しました。どうやってそれを説明できますか?! すでに毛が抜けています。

4

1 に答える 1

0

残念ながら、文字列を「表示」できるという事実は、まったく何も言いません。文字列の割り当てを解除しても、おそらく新しい割り当てと初期化によって上書きされるまで、印刷可能になります。

文字列を印刷した後、文字列を印刷してdelete[]確認してください。

于 2013-01-06T12:17:11.747 に答える