원본 : http://www.debuglab.com/knowledge/newdelete.html
| 1.malloc과 free 보다는 new와 delete를 사용한다. 요점은 이렇습니다 malloc과 free의 문제는 생성자와 소멸자의 존재를 모른다는데 있다. 예를 들어 10개의 스트링 객체의 공간을 얻기위한 두가지 방법 하나는 malloc을 이용한 방법이고 다른하나는 new 를 이용한 방법 string* stringArray1=static_cast<string*>(malloc(10*sizeof(string))); string* stringArray2=new string[10];stringArray1은 10개의 객체를 위한 충분한 메모리를 가리킨다. 하지만 메모리내에는 어떠한 객체도 생성되지 않는다.그리고 아주 특별한 점핑을 통해서만 배열내의 객체들을 초기화 할수 있다. 반면 stringArray2는 10개의 완전히 생성된 string 객체를 가리킨다. stringArray1배열내의 객체들을 용케 초기화했다고 해도,다음과 같은 문제에 봉착하게 된다. free(stringArray1); delete[]stringArray2;stringArray1에 가리켜진 메모리를 해제하지만 어떤 소멸자도 string객체상에서 호출되지 않을 것이다. delete의 경우 당삼 stringArray2의 특정 메모리가 해제되기 전에 배열 내의 각 객체들을 위해 소멸자가 호출된다. -참고 Scott Meyers,EffectiveC++ - 2001.08.13 Smile Seo - |
"C++" 카테고리의 다른 글
- operator new 와 operator delete 작성시 관례를... (0)2007/03/19
- 메모리가 모자랄 경우에 대비한다 (0)2007/03/19
- malloc과 free 보다는 new와 delete를 사용한다. (0)2007/03/19
- based addressing (0)2007/03/17
- stringstream (0)2007/03/08

수안이의 컴퓨터 연구실



Leave your greetings.