Programming/C#
[Programming/C#] 객체 복사: 얕은 복사와 깊은 복사
scii
2020. 9. 8. 11:53
데이터 형식에서 참조 형식과 값 형식이 무엇인지 정확히 알아야 한다.
참조 형식은 힙 영역에 객체를 할당하고 스택에 있는 참조가 힙 영역에 할당된 메모리를 가리킨다. 클래스는 태생이 참조 형식이다. 그래서 객체를 복사할 때 주의해야 한다.
무작정 대입 연산으로 복사되었다고 생각하면 안된다. 왜냐면 참조 형식이기 때문에 변수는 힙 영역에 할당된 객체의 주소 값만을 지니고 있다. 이것을 다른 변수에 대입하면 해당 주소만 넘겨주는 꼴이된다. 그럼 그 다른 변수도 힙 영역의 주소를 가리키게되므로 서로 공유된 메모리를 갖게된다.
이렇게 객체를 복사할 때 참조만 살짝 복사하는 것을 "얕은 복사 (shallow copy)"라고 한다.
원본으로부터 별도의 힙 공간에 객체를 복사하는 것은 "깊은 복사 (deep copy)"라고 한다. 깊은 복사는 C#에서 구문으로 제공하지 않는다. 그러므로 깊은 복사는 프로그래머가 별도로 구현해야 한다.
using System;
namespace CSharpExample
{
class MyClass
{
public int myfield1;
public int myfield2;
public MyClass DeepCopy()
{
MyClass newCopy = new MyClass();
newCopy.myfield1 = this.myfield1;
newCopy.myfield2 = this.myfield2;
return newCopy;
}
}
class MainApp
{
static int Main(string[] args)
{
Console.WriteLine("shallow copy");
{
MyClass source = new MyClass();
source.myfield1 = 10;
source.myfield2 = 20;
MyClass target = source;
target.myfield2 = 55;
Console.WriteLine($"{source.myfield1} {source.myfield2}");
Console.WriteLine($"{target.myfield1} {target.myfield2}");
}
Console.WriteLine("deep copy");
{
MyClass source = new MyClass();
source.myfield1 = 10;
source.myfield2 = 20;
MyClass target = source.DeepCopy();
target.myfield2 = 55;
Console.WriteLine($"{source.myfield1} {source.myfield2}");
Console.WriteLine($"{target.myfield1} {target.myfield2}");
}
return 0;
}
}
}
/* 결과
shallow copy
10 55
10 55
deep copy
10 20
10 55
*/