释放内存的时候为何程序崩溃了
class CMyStack{
public:
CMyStack(int iNum);
void SetNum(int iNum)
{
m_pData[iCount++] = iNum;
}
~CMyStack()
{
delete []m_pData;
m_pData = NULL;
}
private:
int *m_pData;
int iCount;
};
CMyStack::CMyStack(int iNum)
{
m_pData = new int(iNum);
iCount = 0;
}
void main()
{
CMyStack *pMyStack = new CMyStack(2);
for (int i=0;i<2;i++)
{
pMyStack->SetNum(i);
}
delete pMyStack;
}
//程序意图就是对私有数据成员m_pData 开辟空间 并且存入数据,但是释放时为何出错了?难道不能这样用吗?