는 :개체의 동적 배열에서 delete []를 호출해도 메모리가 할당 해제되지 않습니다? 다음 코드에서
int main(int argc,char * argv[]){
int * ptr;
ptr = 0; // tried also with NULL , nothing changes
ptr = new int[10]; // allocating 10 integers
ptr[2] = 5;
ptr[15] = 15; // this should cause error (seg fault) - but it doesn't
cout << ptr[2] << endl;
cout << ptr[15] << endl; // no error here
delete [] ptr;
cout << ptr[2] << endl; // prints the value 5
cout << ptr[15] << endl; // prints the value 15
}
실행의 결과는 : 만 할당하고있어 경우
-
어떻게 인덱스 번호 (15)를 가진 요소가 존재할 수 10?5 15 5 15
- 왜 전체 배열을 할당 해제 한 후에도 포인터에 값이 남아 있습니까?
int * ptr; ptr = 0; ptr = new int; *ptr = 5; cout << *ptr << endl; delete ptr ; cout << *ptr << endl;
결과는 정상입니다 : GCC 4.7.2과에 GCC 4.1.2로
5 0
을 테스트
는이 같은 하나의 할당으로 삭제 시도 fedora 17과 다른 플랫폼 (SLC5 - Red Hat 기반 리눅스)을 컴파일러에 의존하지 않도록합니다. 여기서 내가 뭘 잘못하고 있니?
제 생각에는 허용 된 대답 [읽어야합니다] (http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) (질문이 다릅니다. 조금 있지만 동일한 인수가 적용될 수 있습니다.) – Lol4t0