2009-04-18 2 views

답변

12
// allocate memory for 200 chars 
// p points to the begining of that 
// block 
char *p = new char[200]; 
// we don't know if allocation succeeded or not 
// no null-check or exception handling 

// **Update:** Mark. Or you use std::no_throw or set_new_handler. 
// what happens next is not guranteed 

// p1 now points to the same location as p 
char *p1 = p; 

// another pointer to char which points to the 
// 100th character of the array, note that 
// this can be treated as a pointer to an array 
// for the remaining 100-odd elements 
char *p2 = &p[100]; 

// free the memory for 200 chars 
delete [] p1; 

// **Update:** Doug. T 
// [...] p and p2 are now pointing to freed memory 
// and accessing it will be undefined behavior 
// depending on the executing environment. 
+0

들으 :) 알 필요가 아무것도 아니지만, 처음 100을 삭제하는 방법은 없나요? – Michael

+2

다시 할당 할 수 있습니다. 정상적인 방법은 크기가 100 바이트 인 새 블록을 할당 한 다음 두 번째 100 바이트를 복사하는 것입니다. –

+2

아니요, 파티션을 삭제할 수 없습니다. 두 번째로 새로운 전화를 걸고 원본을 저장하고 지우고 자하는 부분을 복사하십시오. – dirkgently

관련 문제