c++에서 오브젝트 리턴 시 자동으로 삭제되는 문제
글쓴이: keoldn1 / 작성시간: 금, 2012/03/30 - 12:53오전
행렬 관련 프로그램을 작성하고 있습니다.
연산자 오버로딩으로 연산을 수행한 후, 결과 객체를 리턴하고자 합니다. 그런데 리턴이 되기 전에 destructor가 호출되서 메모리를 다 정리해버리더군요. 그래서 리턴 객체의 함수를 호출하면 에러가 발생합니다. 어떻게 해결할 수 있을까요??....
class matrix{ public: int **mat; matrix(int size_){size=size_;mat=NULL;create();} ~matrix(); void create(int size_=0); int& operator()(int i,int j) const{return *(*(mat+j)+i);} friend matrix& operator+(const matrix& lop,const matrix& rop); }; matrix::~matrix(){ int cnt=size; while(cnt--) free(*(mat+cnt)); free(mat); } void matrix::create(int size_){ int sz; int cnt=size; if(size_) {sz=size_; size=size_;} else {sz=size;} if(mat==NULL){ mat=(int **)malloc(size*sizeof(int *)); while(sz--) *(mat+sz)=(int *)malloc(size*sizeof(int)); } else{ while(cnt--) free(*(mat+cnt)); free(mat); mat=(int **)malloc(size*sizeof(int *)); while(sz--) *(mat+sz)=(int *)malloc(size*sizeof(int)); } } void matrix::print(){ for(int j=0;j<size;j++) { for(int i=0;i<size;i++) printf("%d ",(*this)(i,j)); printf("\n"); } } matrix& operator+(const matrix& lop,const matrix& rop){ matrix ret(lop.size); for(int i=0;i<lop.size;i++) for(int j=0;j<lop.size;j++) ret(i,j)=lop(i,j)+rop(i,j); return ret; } int main(int argc,char **argv){ matrix mat; matrix mat2; .......... .......... (mat+mat2).print(); }
Forums:
gilgil.net
본 메소드에서 보면 return object는 method 내부에 있는 ret라는 객체인데요, 이 놈은 local stack에서 생성이 되었다가 method가 종료되는 시점에 객체 해제가 됩니다. 해제된 object를 reference로 받아서 사용(main에서 (mat+mat2).print() 를 하게 되니, 당연히 비정상 작동을 하게 될 것입니다.
[해결 방안]
1. return type을 reference 혹은 pointer로 반환을 하려면 return 객체가 method 종료되는 시점에 해제되지 않도록 한다. 예를 들어 return되는 object를 local에 두지 말고 클래스의 member로 두도록 한다.
2. operator+ 의 return type을 reference나 pointer type으로 하지 말고 그냥 그 type 그대로 반환해 준다.
matrix operator+(const matrix& lop,const matrix& rop){ // & 를 뺄 것.
관련된 테스트를 예전에 해 본 적이 있으니 참고하시기 바랍니다.
http://www.gilgil.net/10970
www.gilgil.net
감사합니다
2번째 방법으로 하니깐 되네요 ㅠㅠㅠㅠㅠ 거의 몇시간 걸렸는데.. 간단하네요ㅠㅠ..
댓글 달기