C++의 delete에 대해 질문..

익명 사용자의 이미지

제가 연습삼아 Linked List구현을 하면서 생긴 의문점인데..
다음과 같은 코드에서..
...
void List del()
{
Node* temp = head;
Node* temp2;
while(temp -> next) {
temp2 = temp;
temp = temp -> next;
}
temp2 -> next = 0;
delete temp;
cout<< temp -> data< }

대강 이런 식으로 링크드 리스트 끝의 노드를 지우는 함수인데..
위에서 delete temp로 끝 노드를 지웠음에도 그 노드의 데이터가 존재하더군요..-_-;
delete가 제대로 동작안해서 메모리반환이 잘 이루어지지 않는다는 이야기인데..
왜 이런건지..
제가 뭘 실수 한건가요?

익명 사용자의 이미지

안녕하세요, ...

생각나는 데로 대충 설명해 볼께요.
제가 드리는 설명이 확실이 올바르다고는 책임못집니다^^

delete 를 했으면 해당 메모리가 지워지는 것이 아닌가요?
특별한 오버로딩을 하지 않았으면, 아마 지워졌을 것이니다.

근데, node = new char [sizeof(LinkedList)]; 와 같이 메모리를 할 당했
으면,
delete [] node;
와 같이 메모리를 해제해야 올바른 것일거에요. 물론 delete node; 와 같
이 해도 컴파일러가 알아서 메모리 해제 코드를 생성하겠지만, ....

소스에 이상이 있는지는 모르겠구요....

이론적으로 따지면, ...
메모리를 해제 한다고 해서, 그 메모리 영역을 컴퓨터 영역에서 잘라내어
버리는 것은 아닐거에요.
즉, 지워진 노드는 (temp 노는) 여전히 메모리의 특정 영역을 가리키고 있
다는 말이되지요.
그 메모리 영역이 쓰레기 값을 가지든지, 아니면, 이전에 메모리를 해제하
기 전의 값을 그대로 가리키고 있던지... 알수는 없죠. (혹시, 메모리를
해제하면, 그 영역의 값을 0으로 채우는지 모르겠네요. 수업시간에 배운
것 같은 데, 기억이 가물 가물 해서...)
어쨌던, 지워진 노드 엮시 값을 가지고 있으니까, 출력을 하면 그 값이 출
력되지요.

따라서 링크드 리스트를 해제할 때에는

delete[] temp->name; temp->name = NULL;
delete[] temp->value; temp->value = NULL;
delete[] temp; temp = NULL;

와 같이 하는 것이 올바르지 않을까요?

물론 지워진 노드를 다시 쓰지 않는다는 확신이 있으면, 그냥 temp 만 해
제를 해도 괜찮겠지만요....

제가 쓰는 링크드 리스트의 노드를 하나 해제하는 소스를 보여드릴께요.

/*
void FreeNode(LinkEntry *node)

Description
Frees the input node.

Warning
This function just deletes(deallocates) a node in the list.
So, before calling this fuction,
make sure that the input 'node' has been removed
from the list.
*/
void CLinkedListFreeNode(LinkEntry *node)
{
if (node == NULL) return;
if (node->name) delete [] node->name;
if (node->value) delete [] node->value;
free(node);
}

Tree wrote..
제가 연습삼아 Linked List구현을 하면서 생긴 의문점인데..
다음과 같은 코드에서..
...
void List del()
{
Node* temp = head;
Node* temp2;
while(temp -> next) {
temp2 = temp;
temp = temp -> next;
}
temp2 -> next = 0;
delete temp;
cout<< temp -> data<<endl; //여기서 지웠음에 도 데이터가 출력됨..
}

대강 이런 식으로 링크드 리스트 끝의 노드를 지우는 함수인데..
위에서 delete temp로 끝 노드를 지웠음에도 그 노드의 데이터가 존재 하 더군요..-_-;
delete가 제대로 동작안해서 메모리반환이 잘 이루어지지 않는다는 이 야 기인데..
왜 이런건지..
제가 뭘 실수 한건가요?

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.