c++在总结类实例时崩溃
C++ crash when summarazing class instances
这是一个简单的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
为:World
调用的构造函数调用c++的构造函数
调用的构造函数:overwrite this
操作符+要求'Hello '添加:World
默认构造函数名为
调用的析构函数:Hello World
操作符+调用'├РРРРР■'来添加:of c++
默认构造函数名为
为:├РРРРР■of c++
调用析构函数将'overwrite this'的赋值操作符从
移动到
事故
1073741819 (0xC0000005)执行时间:37.566 s
按任意键继续。
您的错误在您的copy constructor
cout << "Copy constructor for '" << Buffer << "' to copy from: " << CopySource.Buffer << endl;
当对象在缓冲区中为NULL时,您正在尝试打印buffer
。
谢谢大家如果有人感兴趣,下面是固定的代码(在copy上添加了move构造函数)
#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
为:World
调用的构造函数调用c++的构造函数
调用的构造函数:overwrite this
操作符+被叫:World
默认构造函数名为
移动构造函数:Hello World
operator+ called: of c++
默认构造函数名为
移动构造函数从:c++的Hello World
相关文章:
- 当回溯以零开始时,如何调试崩溃
- 从C++实例化QML
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 内联映射初始化的动态atexit析构函数崩溃
- 执行函数时导致崩溃的变量
- 在 C++ 中,类实例对象 do memcpy 总是崩溃
- 创建'new'实例可以解决析构函数崩溃问题?
- 共同创建实例在 32 位上崩溃
- 如何在崩溃时管理Qt应用程序的单个实例
- 从实例化为静态本地崩溃的类的构造函数调用QPluginLoader::staticInstance
- 尝试从从 DLL 加载的驱动类实例访问基类向量成员时,程序崩溃
- 类的实例只允许1个方法或程序崩溃
- c++在总结类实例时崩溃
- 当我从类实例调用方法时,我的C程序崩溃了,下面是代码
- c++全局/堆栈实例ctor/dtor调用静态函数时崩溃
- 创建构造函数引用全局变量的类的全局实例时,C++ 程序崩溃