즉, delete를 한 후 해당 포인터는 NULL 값을 가지지 않고, 알 수 없는 임의의 주소를 가지게 되어 해당 값을 참조해버릴 수도 있다.
다음은 관련 소스와 결과
#include <iostream>
class test
{
public:
char ch;
~test() { printf("End!\n"); }
};
class test2
{
public:
char ch2;
~test2() { printf("End!\n"); }
};
int main()
{
test* callFunc = new test();
callFunc ->ch = 'a';
delete callFunc;
test2* callFunc2 = new test2();
printf("%p %p\n",(void*)callFunc,(void*)callFunc2);
callFunc2 -> ch2 = 'b';
printf("%c %c \n",callFunc ->ch ,callFunc2 -> ch2);
}
[결과]
End!
0395BA8 0395BA8
b b
해당 내용과 관련해 어느 정도의 확신은 있었지만 직접 Codeproejct에 질문 글을 올려보았다.
해당 내용 답변 중 일부를 수록하면,
1.
This is one of the anomalies of delete in C++. When an object (or block of memory) is deleted then it is marked as free and may be re-used at some later time. However, the actual memory content, and any pointers to it, will not be physically cleared. If you had followed your delete statement with some other statement that needed to allocate memory, then it is likely that the program would have failed, as callFunc would (probably) be pointing at something that was not a test object.
When deleting objects you should always reset any pointers to
NULL in order to protect yourself from any potential hazards.2.
a pointer to a block of allocated memory is not set to NULL when you delete that block of memory. you need to pay attention to that fact when deleting memory.





