메모리 동적 할당을 조금했는데 점수가 없습니다. new
문으로 일부 메모리를 할당 할 때 포인터가 가리키는 메모리를 파괴 할 수 있어야합니다. delete
을 사용하십시오.왜 아무것도 파괴하지 않습니까?
하지만 시도 할 때이 delete
명령이 포인터가 가리키고있는 공간이 비어 있지 않기 때문에 작동하지 않는 것 같습니다.
을 예로 들어이 코드 정말 기본적인 부분을 보자 :
#include <iostream>
using namespace std;
int main()
{
//I create a pointer-to-integer pTest, make it point to some new space,
// and fulfill this free space with a number;
int* pTest;
pTest = new int;
*(pTest) = 3;
cout << *(pTest) << endl;
// things are working well so far. Let's destroy this
// dynamically allocated space!
delete pTest;
//OK, now I guess the data pTest pointed to has been destroyed
cout << *(pTest) << endl; // Oh... Well, I was mistaking.
return 0;
}
어떤 단서?
예, 멍청한 quieston지만 사소한 것은 아닙니다. – sharptooth