생성자와 동적할당
#include <iostream> const int SIZE=20; class Person public: }; void Person::ShowData() int main() |
결과적으로 객체 p는 main함수 내에서 생성되었으므로, 스택(stack)영역에 할당이 되겠지만, 생성자 내에서 메모리 공간을 동적을 할당하고 있기 때문에, 멤버 변수 name과 phone이 가리키는 메모리 공간은 힙(heap)이 된다.
=>이러한 형태의 초기화가 주는 이점은 메모리 공간을 효율적으로 사용할 수 있다는 것이다.
!!!문제 :!! 메모리 누수(유출)현상
=>해결 ? 소멸자에서 동적 해체!(delete)
#include <iostream> const int SIZE=20; class Person public: }; Person::~Person() void Person::ShowData() int main() |