C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[73178] Re:[질문]C++ 입니다.
StarWagon [starwagon] 3914 읽음    2015-11-24 15:47
EmployeeList 클래스의 멤버 변수 중 employee_list는 Employee 형의 더블 포인터로
EmployeeList 클래스를 생성함과 동시에  Employee* 형을 alloc_employee 만큼 동적으로 생성 합니다.

즉 Employee* 를 담을수 있는 공간이 alloc_employee 만큼 존재한다는 겁니다.
그런데 Employee*를 담을 수 있는 공간이지 Employee를 담을 수 있는 공간은 아닙니다.

void add_employee(Employee* employee); 에서는 생성을 하지 않으며
단지 주소만 employee_list[current_employee]에 복사합니다.
그러기 때문에 add함수 호출 후 delete를 하게되면 employee_list[]가 가리키고 있는 공간이 해제되며,

void print_employee_info() 등을 호출 시 엉뚱한 값을 출력 하거나 접근오류가 발생할 수 있습니다.

EmployeeList 클래스의 소멸자에서 할당된 Employee 의 개수만큼 해제를 하므로
alloc_employee 이하 만큼 add 를 한다면 메모리 누수는 없습니다.














:     int alloc_employee; //할당된 총 직원수
:     int current_employee; //현재 직원수
:     int current_manager; //현재 매니저수
:     Employee **employee_list; //직원데이타
:     Manager **manager_list; //매니저 데이타


하안인 님이 쓰신 글 :
: #include <iostream>
: #include <string>
: using namespace std;
:
: class Employee
: {
: protected:
:     string name;
:     int age;
:
:     string position; // 직책 (이름)
:     int rank; // 순위 (값이 클 수록 높은 순위)
:
: public:
:     Employee(string name, int age, string position, int rank)
:         : name(name), age(age), position(position), rank(rank) {cout << "employee 생성자 호출" << endl;}
:     //복사생성자
:     Employee(const Employee& employee)
:     {
:         cout << "employee 복사생성자 호출" << endl;
:         name = employee.name;
:         age = employee.age;
:         position = employee.position;
:         rank = employee.rank;
:     }
:
:     //디폴트 생성자
:     Employee() {}
:
:     void print_info()
:     {
:         cout << name << "(" << position << "," << age << "==>" << calculate_pay() << "만원" << endl;
:     }
:
:     int calculate_pay()
:     {
:         return 200+rank*50;
:     }
: };
:
: class Manager : public Employee
: {
:     int year_of_service;
: public:
:     Manager(string name,int age,string position,int rank,int year_of_service):year_of_service(year_of_service),Employee(name,age,position,rank){
:         cout << "manager 생성자호출" << endl;
:     }
:     //복사생성자
:     Manager(const Manager& manager) : Employee(manager.name,manager.age,manager.position,manager.rank)
:     {
:         cout << "manager 복사생성자 호출" << endl;
:         year_of_service = manager.year_of_service;
:     }
:     //디폴트생성자
:     Manager():Employee(){}
:
:     int calculate_pay()
:     {
:         return 200+rank*50+5*year_of_service;
:     }
:     void print_info()
:     {
:         cout << name << "(" << position << "," << age << "," << year_of_service << "년차)==>" << calculate_pay() << "만원" << endl;
:     }
: };
:
: class EmployeeList
: {
:     int alloc_employee; //할당된 총 직원수
:     int current_employee; //현재 직원수
:     int current_manager; //현재 매니저수
:     Employee **employee_list; //직원데이타
:     Manager **manager_list; //매니저 데이타
:
: public:
:     EmployeeList(int alloc_employee): alloc_employee(alloc_employee)
:     {
:             employee_list = new Employee* [alloc_employee];
:             manager_list = new Manager*[alloc_employee];
:             current_employee = 0;
:             current_manager = 0;
:     }
:
:     void add_employee(Employee* employee)
:     {
:         // 사실 current_employee 보다 alloc_employee 가 더
:         // 많아지는 경우 반드시 재할당을 해야 하지만, 여기서는
:         // 최대한 단순하게 생각해서 alloc_employee 는
:         // 언제나 current_employee 보다 크다고 생각한다.
:         // (즉 할당된 크기는 현재 총 직원수 보다 많음)
:         employee_list[current_employee] = employee;
:         current_employee ++;
:     }
:     void add_manager(Manager* manager)
:     {
:         manager_list[current_manager] = manager;
:         current_manager++;
:     }
:     int current_employee_num()
:     {
:         return current_employee + current_manager;
:     }
:
:     void print_employee_info()
:     {
:         int total_pay = 0;
:         for(int i = 0; i < current_employee; i ++) {
:             employee_list[i]->print_info();
:             total_pay += employee_list[i]->calculate_pay();
:         }
:         for(int i = 0; i < current_manager; i++){
:             manager_list[i]->print_info();
:             total_pay += manager_list[i]->calculate_pay();
:         }
:         cout << "총 비용 : " << total_pay << "만원 " << endl;
:     }
:     ~EmployeeList()
:     {
:         for(int i = 0; i < current_employee; i ++) {
:             delete employee_list[i];
:         }
:         for(int i = 0; i < current_manager; i++){
:             delete manager_list[i];
:         }
:         delete [] employee_list;
:         delete [] manager_list;
:     }
:
: };
:
: int main()
: {
:     EmployeeList emp_list(10);
:     emp_list.add_employee(new Employee("노홍철",34,"평사원",1));  // ==> new 연산자 사용에 대하여
:     emp_list.add_employee(new Employee("하하",34,"평사원",1));
:     emp_list.add_manager(new Manager("유재석",41,"부장",7,12));
:     emp_list.add_manager(new Manager("정준하",43,"과장",4,15));
:     emp_list.add_manager(new Manager("박명수",43,"차장",5,13));
:     emp_list.add_employee(new Employee("정형돈",36,"대리",2));
:     emp_list.add_employee(new Employee("길",36,"인턴",-2));
:     emp_list.print_employee_info();
:     return 0;
: }
:
: 위의 식에서
: emp_list.add_employee(new Employee("노홍철",34,"평사원",1));
:
: 는
: Employee* em = new Employee("노홍철",34,"평사원",1);
: emp_list.add_employee(em);
: 과 같은것을 알고 있는데요.
:
: 그러면 delete 를 해 주어야 하는것이 맞는건이 아닌가요.
:
: 즉
: delete em; 처럼 해주어야 할것 같은데....
:
: 아니면 생성자를 사용하지 않고서도 사용할 수 있나요.
:
: char *a;   와   char *a = new char* ; 의 차이점..
:
:
:

+ -

관련 글 리스트
73177 [질문]C++ 입니다. 하안인 3880 2015/11/24
73178     Re:[질문]C++ 입니다. StarWagon 3914 2015/11/24
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.