-
카테고리
-
세부 분야
프로그래밍 언어
-
해결 여부
미해결
5:10 부분 결과값이 강의와 약간 다르게 나오는데 원인이 뭔지 모르겠습니다
21.03.08 23:31 작성 조회수 136
1
main.cpp
#include "AutoPtr.h"
#include "Resource.h"
using namespace std;
AutoPtr<Resource> generateResource()
{
AutoPtr<Resource> res(new Resource(3));
return res;
}
int main()
{
{
AutoPtr<Resource> main_res;
main_res = generateResource();
}
return 0;
}
////////////////////////////
AutoPtr.h
#pragma once
#include <iostream>
template<class T>
class AutoPtr
{
public:
T* m_ptr = nullptr;
public:
AutoPtr(T* ptr = nullptr)
:m_ptr(ptr)
{
std::cout << "AutoPtr default constructor" << std::endl;
}
~AutoPtr()
{
std::cout << "AutoPtr destructor" << std::endl;
if (m_ptr != nullptr)
delete m_ptr;
}
T& operator*() const { return *m_ptr; }
T* operator->()const { return m_ptr; }
bool isNull()const { return m_ptr == nullptr; }
AutoPtr(const AutoPtr& a)
{
std::cout << "AutoPtr copy constructor" << std::endl;
//deep copy
m_ptr = new T;
*m_ptr = *a.m_ptr;
}
AutoPtr& operator=(const AutoPtr& a)
{
std::cout << "AutoPtr copy assignement" << std::endl;
if (&a == this)
return *this;
//deep copy
m_ptr = new T;
*m_ptr = *a.m_ptr;
return *this;
}
/////////////////////////////////////
Resource.h
#pragma once
#include <iostream>
class Resource
{
//private:
public:
int* m_data=nullptr;
unsigned m_length = 0;
public:
Resource()
{
std::cout << "Resource default constructed" << std::endl;
}
Resource(unsigned length)
{
std::cout << "Resource length constructed" << std::endl;
this->m_data = new int[length];
this->m_length = length;
}
~Resource()
{
std::cout << "Resource destroyed" << std::endl;
if (m_data != nullptr)
delete[]m_data;
}
Resource(const Resource& res)
{
std::cout << "Resource copy constructed" << std::endl;
Resource(res.m_length);
for (unsigned i = 0; i < m_length; ++i)
m_data[i] = res.m_data[i];
}
Resource& operator =(Resource& res)
{
std::cout << "Resource copy assignment" << std::endl;
if (&res == this)return *this;
if (this->m_data != nullptr)delete[]m_data;
m_length = res.m_length;
m_data = new int[m_length];
for (unsigned i = 0; i < m_length; ++i)
m_data[i] = res.m_data[i];
return *this;
}
void print()
{
for (unsigned i = 0; i < m_length; ++i)
std::cout << m_data[i] << " ";
std::cout << std::endl;
}
};
웬만하면 코드 질문은 스스로 해결하려고 했는데 며칠 고민하면서 디버거로 찍어봐도 중간중간에 뜬금없이 생성자가 호출되거나 다시 복사가 되는 원인을 못찾겠습니다
답변을 작성해보세요.
8
안소
2021.03.10
Debug 모드로 실행하셨기 때문에 그렇습니다. 강의에선 교수님께서 Debug 모드가 아닌 Release 모드로 실행하고 계신데 Debug 모드에선 generateResource 함수가 리턴하는 res 가 임시 객체에 복사되는 과정에서 복사 생성자를 호출 해주는 듯 합니다. 이와 달리 Release 모드로 실행할 땐 res 가 임시 객체에 복사되는 과정에서 복사생성자가 호출되지 않네요.
(함수의 리턴값은 R-value라는 것을 알고 계실겁니다. 리턴값이 임시 공간에 사본으로서 복사된 후 함수 실행이 끝나면서 진짜 리턴값은 사라지죠! 이 R-value 사본을 함수 바깥에서 받게되는 것이구요)
저도 평소에 Debug 모드로 놓고 쓰는지라 이유를 모르겠어서 의아해하다가 https://www.inflearn.com/questions/22630 이 질문글의 댓글 보고 알게되었습니다.
위 링크를 읽어보시면 이해에 도움이 되실 것 같습니다. C++컴파일러는 경우에 따라 복사생성자 호출을 스킵하도록 최적화가 되있는데 디버그 모드보다 더 최적화를 추구하는 릴리즈 모드에선 함수 리턴 값이 임시객체에 복사되는 과정에서의 복사생성자 호출은 아예 생략해주는 것 같네요.
디버그 모드에선 저런 결과가 도출되는 것이 지극히 정상적인 현상이니 걱정안하셔도 될 것 같습니다! 릴리즈 모드로 실행하시면 교수님과 동일한 출력 결과 얻으실 수 있을 것입니다.
답변 1