연결리스트에서 메모리 에러...

sadrove의 이미지

//존재하는 노드의 개수를 비교하는 프로그램
void List::nodeLength()
{
	int cntLength=0;

	ListNode *t;
	t = first;

	while(1){
		cntLength++;

		cout << t->link;
		if(!t->link) break;		//마지막 노드(마지막 노드는 link가 0)이면 카운트 정지
		t = t->link;
	}
	cout << "Node Length = " << cntLength << endl;

	delete t;
}

위 소스는 연결리스트에서 노드의 개수를 알아내는 함수인데요..
메인에서 node1.nodeLength();
이런식으로 호출하면 정상적으로 노드의 개수를 출력합니다..
근데 이상한건 두번 호출하면 즉 아래와 같이..

	node1.nodeLength();
	node1.nodeLength();

두번째 호출되는 부분에서 메모리 위반이라는 에러가 나옵니다..
디버그로 두번째 호출되는 부분의 first의 메모리 위치를 확인했는데..
첫번째 함수가 호출되는 부분과 같더군요..
어디서 메모리 위반이 일어나는 걸까요...ㅠ.ㅠ...
byung82의 이미지

void List::nodeLength() 
{ 
   int cntLength=0; 

   ListNode *t; 
   t = first; 

   while(1){ 
      cntLength++; 

      cout << t->link; 
      if(!t->link) break;      //마지막 노드(마지막 노드는 link가 0)이면 카운트 정지 
      t = t->link; 
   } 
   cout << "Node Length = " << cntLength << endl;

   // delete t; 
} 

이렇게 수정하세요

노드 확인후 다른곳에서 저 노드를 죽여야 합니다.

지금은 단지 확인으로 메모리 주소만 대입했으니 굳이 안죽여도 됩니다.
메모리 leak도 날 이유가 없구요

그럼[/list][/code]

sadrove의 이미지

답변 감사합니다..
님의 말씀대로 하니까 괜찮네요...^^
그런데 사실 잘 이해가 안갑니다..
왜 delete를 하면 에러가 나는건지....
괜찮으시다면 자세히 설명 좀 해주실 수 있을까요...
좋은 하루 되세요..

exsider의 이미지

delete 를 하면 2번째 호출에서 에러가 나는게 당연합니다.

처음 List::nodeLength() 를 호출했을 때

delete t;

를 실행할 때 t 는 리스트의 마지막 노드를 가리키고 있습니다.
그런데 이걸 삭제해 버리니 리스트의 구조가 깨지게 됩니다.
(삭제한 노드 바로앞의 노드가 새로운 끝이 되겠지만 그것의 link가
null이 아니므로 두번째 호출에서 재참조하려다가 오류가 나는 것입니다.)

댓글 달기

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