Make Unreal REAL.
article thumbnail
Published 2023. 1. 20. 21:37
깊은 복사와 얕은 복사 C++/기타

 

깊은 복사 (Deep copy)

  • 다른 Memory chunk를 만들어 값을 복사한다.
  • 한 쪽에서 값을 수정해도 다른 쪽에 영향을 미치지 않는다.

 

얕은 복사 (Shallow copy)

  • 같은 Memory chunk를 가리키게 된다.
  • 한 쪽에서 값을 수정하면 다른 쪽에 영향을 미친다.

 

#include <iostream>

#define ARR_SIZE 5

using namespace std;

void main()
{
    int* p;
    int arr[ARR_SIZE] = {1, 2, 3, 4, 5};
    
    // 얕은 복사
    p = arr;
    
    // 깊은 복사
    p = new int[ARR_SIZE];
    for (int i = 0; i < ARR_SIZE; ++i)
    	p[i] = arr[i];
        
    delete[] p;
    p = nullptr;
}

 

'C++ > 기타' 카테고리의 다른 글

펑터(Functor)  (0) 2023.01.21
템플릿(Template)  (0) 2023.01.20
std::iterator와 std::reverse_iterator  (0) 2023.01.20
메모리 누수(Memory Leak)  (0) 2023.01.19
nullptr과 NULL  (0) 2023.01.19
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그