首页
论坛
课程
招聘
[求助]请问大神们p255页12行的bug出在哪里?
2017-10-28 13:46 1105

[求助]请问大神们p255页12行的bug出在哪里?

2017-10-28 13:46
1105

//class.h

ifndef CLASS_H_

#define CLASS_H_

class CMyString

{

public: CMyString();

CMyString(CMyString& obj);

CMyString(char *pstrBuff);

~CMyString();

void SetString(char *pstrBuff);

char* GetString(); int GetStringLen();

private: char *m_pstrBuff;

};

#endif


#include "class.h" #include <iostream> CMyString::CMyString() { m_pstrBuff = NULL; } CMyString::CMyString(CMyString& obj) { m_pstrBuff = NULL; this->SetString(obj.GetString()); } CMyString::CMyString(char *pstrBuff) { m_pstrBuff = NULL; SetString(pstrBuff); } CMyString::~CMyString() { if (m_pstrBuff != NULL) { delete [] m_pstrBuff; m_pstrBuff = NULL; } } void CMyString::SetString(char *pstrBuff) { if (m_pstrBuff != NULL) { delete m_pstrBuff; m_pstrBuff = NULL; } int nLen = strlen(pstrBuff); m_pstrBuff = new char[nLen + sizeof(char)]; do { m_pstrBuff[nLen] = pstrBuff[nLen]; nLen--; } while (nLen >= 0); } char* CMyString::GetString() { return m_pstrBuff; } int CMyString::GetStringLen() { int nLen = 0; do { nLen++; } while (m_pstrBuff[nLen]); return nLen; }


#include "class.h" #include <string.h> #include <iostream> CMyString ShowString(CMyString MyString) { CMyString MyStringRet; char szBuff[50] = {0}; scanf("%50s", szBuff); printf("%s\r\n", MyString.GetString()); MyStringRet.SetString(szBuff); return MyStringRet; } int main(int argc, char* argv[]) { char szBuff[50] = {0}; scanf("%50s", szBuff); CMyString * pMyString = NULL; CMyString MyString(szBuff);//Wanderer CMyString MyStringRet = ShowString(MyString); printf("%s\r\n", MyStringRet.GetString()); // 堆对象的使用 pMyString = new CMyString[2]; if (pMyString == NULL) { return -1; } pMyString[0].SetString(MyStringRet.GetString()); pMyString[1].SetString(MyString.GetString()); printf("%s %s\r\n", (*pMyString).GetString(), (*(pMyString+1)).GetString()); if (pMyString != NULL) { delete [] pMyString; // BUG所在处 pMyString = NULL; } return 0; }


[注意] 欢迎加入看雪团队!base上海,招聘安全工程师、逆向工程师多个坑位等你投递!

收藏
点赞0
打赏
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回