C++ C++;损坏的堆
当main方法返回时,我的程序似乎抛出了一个关于损坏堆的符文时异常。我已经采取了适当的预防措施以避免这种情况发生,包括复制构造函数。有人能解释一下为什么会发生这种情况吗 MyString.cppC++ C++;损坏的堆,c++,memory,malloc,free,realloc,C++,Memory,Malloc,Free,Realloc,当main方法返回时,我的程序似乎抛出了一个关于损坏堆的符文时异常。我已经采取了适当的预防措施以避免这种情况发生,包括复制构造函数。有人能解释一下为什么会发生这种情况吗 MyString.cpp #include "MyString.h" #include <cstdio> #include <Windows.h> MyString::MyString() { str = (char*)malloc(sizeof(char)); *str = '\0';
#include "MyString.h"
#include <cstdio>
#include <Windows.h>
MyString::MyString() {
str = (char*)malloc(sizeof(char));
*str = '\0';
}
MyString::MyString(char* src) {
int size = sizeof(char)*(strlen(src) + 1);
str = (char*)malloc(size);
strcpy_s(str, size, src);
}
MyString MyString::operator+(char* add) {
int addSize = sizeof(char)*strlen(add);
int fullSize = sizeof(char)*(strlen(str) + 1) + addSize;
str = (char*)realloc(str, fullSize);
char* temp = str;
temp += strlen(str);
strcpy_s(temp, addSize + 1, add);
return *this;
}
MyString::~MyString() {
if (str)
free(str);
}
MyString::MyString(const MyString &arg) {
int size = sizeof(char) * (strlen(arg.str) + 1);
str = (char*)malloc(size);
strcpy_s(str, size, arg.str);
}
#包括“MyString.h”
#包括
#包括
MyString::MyString(){
str=(char*)malloc(sizeof(char));
*str='\0';
}
MyString::MyString(char*src){
int size=sizeof(char)*(strlen(src)+1);
str=(char*)malloc(size);
strcpy_s(str、size、src);
}
MyString MyString::operator+(char*add){
int addSize=sizeof(char)*strlen(add);
int fullSize=sizeof(char)*(strlen(str)+1)+addSize;
str=(char*)realloc(str,全尺寸);
char*temp=str;
温度+=strlen(str);
strcpy_s(临时,添加大小+1,添加);
归还*这个;
}
MyString::~MyString(){
如果(str)
自由基(str);
}
MyString::MyString(constmystring&arg){
int size=sizeof(char)*(strlen(arg.str)+1);
str=(char*)malloc(size);
strcpy_s(str,size,arg.str);
}
main.cpp
#include <iostream>
#include "MyString.h"
using namespace std;
int main(int argc, char *argv[]) {
MyString test = MyString("hello!");
test = test + " world";
cout << test.toString() << endl;
cout << strlen(test.toString()) << endl;
system("pause");
return 0; //runtime error here
}
#包括
#包括“MyString.h”
使用名称空间std;
int main(int argc,char*argv[]){
MyString test=MyString(“你好!”);
测试=测试+世界;
cout我正在根据@user4581301的建议修改我的帖子:
您必须修改加法运算符重载,以便它生成一个新对象,并实现如下的assignment运算符重载:
MyString operator+(char* add) const {
int thisSize = sizeof(char)*strlen(str);
int addSize = sizeof(char)*(strlen(add) + 1);
int fullSize = thisSize + addSize;
char* tempStr = (char*)malloc(fullSize);
strcpy_s(tempStr, fullSize, str);
strcpy_s(tempStr + thisSize, fullSize, add);
return MyString(tempStr);
}
MyString& operator=(const MyString& assign){
int assignSize = sizeof(char)*(strlen(assign.str) + 1);
str = (char*)realloc(str, assignSize);
strcpy_s(str, assignSize, assign.str);
return *this;
}
你必须了解
使用隐式赋值运算符,当旧对象被销毁时,新对象将使用已释放的指针,稍后它将再次尝试释放该指针。toString在何处定义“MyString.h”中的内容?我鼓励使用new
和delete
而不是malloc
和free
。应该在test=test+“world”
得到编译器警告,因为“world”
是常量字符*
,而不是char*
。与MyString test=MyString(“你好!”)一样
通常我会同意@grigor,但是他们后来使用的realloc
技巧有点恶心。很可能你是对的。test=test+“world”;
很可能是致命的,但我们需要看看操作符=
的实现,如果有的话。复制构造函数和析构函数看起来有三个规则,我们还没有看到赋值操作符。在那之前,我们能做的就是猜测。离题:有一个非常好的技巧,使用+=
来实现+
>此处包括: