c++공부중 이런 현상이 왜 일어나는지, 어떻게 해결하는지 궁금합니다

rkd1306의 이미지

#include <iostream>
using namespace std;
class Book {
public:
	Book();
	Book(const char t[], const char pub[], int y, int pr);
	void print();
	~Book();
private:
	char* title;
	char* publisher;
	int year;
	int price;
};
Book::Book() {
	title = new char[20];
	publisher = new char[20];
	year = 0;
	price = 0;
}
Book::Book(const char t[], const char pub[], int y, int pr) {
	title = new char[20];
	for (int i = 0; i < 20; i++)
		title[i] = t[i];
	publisher = new char[20];
	for (int i = 0; i < 20; i++)
		publisher[i] = pub[i];
	year = y;
	price = pr;
}
Book::~Book() {
	delete[] title;
	delete[] publisher;
}
void Book::print() {
	cout << "Title :" << title << "\nPublisher : " << publisher << "\nYear : " << year << "\nPrice : " << price << endl;
}
int main() {
	Book book1("C++ Primer", "Addison-Wesley", 1989, 39000);
	book1.print();
	Book book2 = book1;
	book2.print();
	Book book3(book1);
	book3.print();
}

이 코드에서 의도한대로 작동은 잘 됩니다만, 첨부 사진같은 경고창이 뜨네요... 해결할 수 있는 방안을 알려주실수 있나요?

File attachments: 
첨부파일 크기
Image icon 캡처.JPG69.73 KB
익명 사용자의 이미지

1. 의도한 대로 작동하지 않습니다.

프로그램이 너무 간단해서 종료되기 직전쯤에야 문제가 생기는 바람에 문제가 없는 것처럼 보일 뿐이죠.

문제의 요점은 book1을 복사해서 book2와 book3을 생성할 때, book1이 가지고 있던 할당된 메모리를 book2와 book3도 같이 공유하게 된다는 겁니다.

직접 title과 publisher 필드의 값을 찍어 보면 금방 알 수 있습니다.

#include <iostream>
using namespace std;
 
class Book {
public:
	Book();
	Book(const char t[], const char pub[], int y, int pr);
	void print();
	~Book();
private:
	char* title;
	char* publisher;
	int year;
	int price;
};
 
Book::Book() {
	title = new char[20];
	publisher = new char[20];
	year = 0;
	price = 0;
}
 
Book::Book(const char t[], const char pub[], int y, int pr) {
	title = new char[20];
	for (int i = 0; i < 20; i++)
		title[i] = t[i];
	publisher = new char[20];
	for (int i = 0; i < 20; i++)
		publisher[i] = pub[i];
	year = y;
	price = pr;
}
 
Book::~Book() {
#ifndef NDEBUG
    cerr << "\tdeleting title: " << static_cast<void *>(title) << "\n\tdeleting publisher: " << static_cast<void *>(publisher) << endl;
#endif
	delete[] title;
	delete[] publisher;
}
 
void Book::print() {
	cout << "Title :" << title << "\nPublisher : " << publisher << "\nYear : " << year << "\nPrice : " << price << endl;
#ifndef NDEBUG
    cerr << "\ttitle: " << static_cast<void *>(title) << "\n\tpublisher: " << static_cast<void *>(publisher) << endl;
#endif
}
 
int main() {
	Book book1("C++ Primer", "Addison-Wesley", 1989, 39000);
	book1.print();
	Book book2 = book1;
	book2.print();
	Book book3(book1);
	book3.print();
}

이런 상황을 보통 얕은 복사(shallow copy)가 이루어졌다고 하며, 대충 보이는 문제만 두 가지입니다.

1) book1, book2, book3 중 어느 하나의 title 혹은 publisher 문자열의 내용을 바꾸려고 시도할 경우, 세 객체가 모두 영향을 받게 됩니다. 한 문자열을 공유하고 있으니까요.

주어진 코드에서는 그런 일을 하고 있지 않으니 아무 문제가 없어 보이지요.

2) book1, book2, book3 중 가장 먼저 소멸되는 객체가 title과 publisher 문자열 메모리를 해제해 버리기 때문에, 이후 남아있는 객체들은 delete된 메모리를 가리키게 됩니다.

delete되었기 때문에 값을 유지하고 있기는 커녕 언제 다른 용도로 할당되어 재사용 될지 알 수 없기도 하고, 또 나중에 남아있는 객체들도 소멸되면서 이미 해제된 메모리에 재차 delete가 일어나게 되겠죠.

이런 걸 보통 double-free 문제라고 하는데, 두 번째 delete에서는 동적 메모리 관리에 필요한 내부 자료 구조가 이미 흐트러진 상황이기 때문에 어떤 일이 일어날지 파악하기 매우 어렵습니다.

"최선의 경우" 에러가 발생하여 프로그램이 즉시 종료되겠죠.

2. 해결책은...

1) 가장 straightforward한 솔루션은 title과 publisher를 std::string field로 만드는 것입니다.

이러면 Book 객체는 심지어 non-default 소멸자를 가질 필요도 없습니다. std::string 객체는 알아서 잘 복사, 이동(since C++11), 소멸됩니다.

2) 공부 목적이라면, Book 객체가 복사, 이동(since C++11), 소멸될 때 각 객체가 자신만의 title, publisher를 위한 메모리를 소유 및 해제할 수 있도록 해 주어야 합니다.

복사 생성자, 복사 대입 연산자, 이동 생성자(since C++11), 이동 대입 연산자(since C++11)를 모두 작성해보세요.

3) 그 밖에 뭐 스마트 포인터를 쓴다던가 다른 방법도 있기는 하겠네요.

rkd1306의 이미지

아직 수행이 부족한가 봅니다. 친절한 답변 감사합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.