-
-
[求助]请问大神们p255页12行的bug出在哪里?
-
发表于: 2017-10-28 13:46 2540
-
//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; }
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课
赞赏
- [求助]请问大神们p255页12行的bug出在哪里? 2541
- [求助]进入函数时的代码问题 求指点 3198
- [求助]p66页and edx,7问题求助 3357