汇总类实例时C ++崩溃

时间:2015-07-29 06:24:02

标签: c++ c++11 constructor crash copy

这是一个简单的Hello World代码,它应该使用复制构造函数来汇总对象

下面是

生成的代码和输出

我想崩溃是因为析构函数调用它不应该在哪里(或者我的C ++学习书的作者没有预料到的),但可能你可以给我一些建议

我使用代码块的默认GNU GCC编译器和额外选项-std = c ++ 11 -fno-elide-constructors(在这种情况下它们无关紧要)

#include <iostream>
#include <string>
#include <cstring>
#include <sstream>

using namespace std;

class MyString
{
private:
  char* Buffer;

  MyString(): Buffer(NULL)
  {
    cout << "Default constructor called" << endl;
  }

public:
  MyString( const char* InitialInput )
  {
    cout << "Constructor called for: " << InitialInput << endl;
    if(InitialInput != NULL)
    {
      Buffer = new char [strlen(InitialInput)+1];
      strcpy( Buffer, InitialInput );
    }
    else
      Buffer = NULL;
  }

  MyString operator+ (const MyString& AddThis)
  {
    cout << "operator+ called for '" << Buffer << "' to add: " << AddThis.Buffer << endl;
    MyString NewString;
    if (AddThis.Buffer != NULL)
    {
      NewString.Buffer = new char[GetLenght() + strlen( AddThis.Buffer ) + 1];
      strcpy( NewString.Buffer, Buffer );
      strcat( NewString.Buffer, AddThis.Buffer );
    }
  }

  MyString& operator= (const MyString& CopySource)
  {
    cout << "Copy assignment operator for '" << Buffer << "' to copy from: " << CopySource.Buffer << endl;
    if ((this != &CopySource) && (CopySource.Buffer != NULL))
    {
      if (Buffer != NULL)
        delete[ ] Buffer;
      // гарантирует глубокую копию с предварительным
      // резервированием собственного буфера
      Buffer = new char [strlen(CopySource.Buffer) + 1];
      // копирование оригинала в локальный буфер
      strcpy(Buffer, CopySource.Buffer);
    }
    return *this;
  }

  MyString( const MyString& CopySource )
  {
    cout << "Copy constructor for '" << Buffer << "' to copy from: " << CopySource.Buffer << endl;
    if(CopySource.Buffer != NULL)
    {
      Buffer = new char [strlen(CopySource.Buffer)+1];
      strcpy(Buffer,CopySource.Buffer);
    }
    else
      Buffer = NULL;
  }

  ~MyString()
  {
    cout << "Destructor called for: " << Buffer << endl;
    if( Buffer != NULL )
      delete [] Buffer;
  }

  int GetLenght()
  {
    return strlen(Buffer);
  }

  operator const char*()
  {
    return Buffer;
  }
};

int main( )
{
  MyString Hello("Hello ");
  MyString World("World");
  MyString CPP(" of C++");

  MyString sayHelloAgain ("overwrite this");
  sayHelloAgain = Hello + World + CPP;

  return 0;
}

输出

构造函数调用:Hello
构造函数要求:世界
构造函数要求:C ++
构造函数要求:覆盖此
operator +调用'Hello'来添加:World
默认构造函数为
析构函数要求:Hello World
操作员+要求'├РРРРР■'添加:C ++
默认构造函数为
析构函数要求:C ++的├РРРРР■ 移动赋值运算符以'覆盖此'来移动:
CRASH
进程返回-1073741819(0xC0000005)执行时间:37.566 s
按任意键继续。

2 个答案:

答案 0 :(得分:0)

您的错误位于cout << "Copy constructor for '" << Buffer << "' to copy from: " << CopySource.Buffer << endl;

的这一行
buffer

当对象在缓冲区中为NULL时,您正在尝试打印file.ContentLength

答案 1 :(得分:0)

谢谢大家 如果有人感兴趣(使用复制的移动构造函数)

,则下面是固定代码
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>

using namespace std;

class MyString
{
private:
  char* Buffer;

  MyString(): Buffer(NULL)
  {
    cout << "Default constructor called" << endl;
  }

public:
  MyString( const char* InitialInput )
  {
    cout << "Constructor called for: " << InitialInput << endl;
    if(InitialInput != NULL)
    {
      Buffer = new char [strlen(InitialInput)+1];
      strcpy( Buffer, InitialInput );
    }
    else
      Buffer = NULL;
  }

  MyString operator+ (const MyString& AddThis)
  {
    cout << "operator+ called: " << AddThis.Buffer << endl;
    MyString NewString;
    if (AddThis.Buffer != NULL)
    {
      NewString.Buffer = new char[GetLenght() + strlen( AddThis.Buffer ) + 1];
      strcpy( NewString.Buffer, Buffer );
      strcat( NewString.Buffer, AddThis.Buffer );
    }
    return NewString;
  }

  MyString& operator= (const MyString& CopySource)
  {
    cout << "Copy assignment operator to copy from: " << CopySource.Buffer << endl;
    if ((this != &CopySource) && (CopySource.Buffer != NULL))
    {
      if (Buffer != NULL)
        delete[ ] Buffer;
      Buffer = new char [strlen(CopySource.Buffer) + 1];
      strcpy(Buffer, CopySource.Buffer);
    }
    return *this;
  }

  MyString( const MyString& CopySource )
  {
    cout << "Copy constructor to copy from: " << CopySource.Buffer << endl;
    if(CopySource.Buffer != NULL)
    {
      Buffer = new char [strlen(CopySource.Buffer)+1];
      strcpy(Buffer,CopySource.Buffer);
    }
    else
      Buffer = NULL;
  }

  MyString( MyString&& MoveSource)
  {
    cout << "Move constructor to move from: " << MoveSource.Buffer << endl;
    if(MoveSource.Buffer != NULL)
    {
      Buffer = MoveSource.Buffer;
      MoveSource.Buffer = NULL;
    }
  }

  MyString& operator= (MyString&& MoveSource)
  {
    cout << "Move assignment operator to move from: " << MoveSource.Buffer << endl;
    if ((this != &MoveSource) && (MoveSource.Buffer != NULL))
    {
      if (Buffer != NULL)
        delete[ ] Buffer;
      Buffer = new char [strlen(MoveSource.Buffer) + 1];
      strcpy(Buffer, MoveSource.Buffer);
    }
    return *this;
  }

  ~MyString()
  {
    if( Buffer != NULL )
      delete [] Buffer;
  }

  int GetLenght()
  {
    return strlen(Buffer);
  }

  operator const char*()
  {
    return Buffer;
  }
};

int main( )
{
  MyString Hello("Hello ");
  MyString World("World");
  MyString CPP(" of C++");

  MyString sayHelloAgain ("overwrite this");
  sayHelloAgain = Hello + World + CPP;

  return 0;
}

构造函数调用:Hello
构造函数要求:世界
构造函数要求:C ++
构造函数要求:覆盖此
operator + called:世界
默认构造函数为
移动构造函数移动:Hello World
operator + called:of C ++
默认构造函数为
移动构造函数来自:C ++的Hello World 移动赋值运算符以从:C ++的Hello World

移动