C++中动态内存分配引发问题的解决方案
下面图老师小编跟大家分享C++中动态内存分配引发问题的解决方案,一起来学习下过程究竟如何进行吧!喜欢就赶紧收藏起来哦~
Wrong.h:
#ifndef WRONG_H_
#define WRONG_H_
class Wrong
{
private:
char * str; //存储数据
int len; //字符串长度
public:
Wrong(const char * s); //构造函数
Wrong(); // 默认构造函数
~Wrong(); // 析构函数
friend ostream & operator<<(ostream & os,const Wrong& st);
};
#endif
Wrong.cpp:
#include <iostream>
#include <cstring>
#include "wrong.h"
using namespace std;
Wrong::Wrong(const char * s)
{
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
}//拷贝数据
Wrong::Wrong()
{
len =0;
str = new char[len+1];
str[0]='