코드를 하나짰는데요 메모리 누수가 일어나는것 같네요

익명 사용자의 이미지

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
 
 
struct node
{
	int level;
	int key;
	int ch_key;
	struct node *left;
	struct node *right;
};
 
struct node*root = NULL;
void preorder(struct node*root);
void insert(struct node *root, int level, int key, int ch);
struct node * makeNode(int level, int key, int ch);
 
int main()
{
	int key, level, ch_key;
	char child[5];
	int i = 0;
	struct node*newnode = malloc(sizeof(struct node));
 
	while (i<10)
	{
		printf("for example you want to insert key 100 and level 1 and child_key 200 ----> 1 100 200\n");
		scanf("%d %d  %s", &level, &key, child);
 
		if (child == "NULL")
		{
			ch_key = '\0';
		}
		else ch_key = atoi(child);
 
		if (i == 0)
		{
			newnode->ch_key = ch_key;
			newnode->level = level;
			newnode->key = key;
 
			root = newnode;
		}
 
		else
			insert(root, level, key, ch_key);
		i++;
	}
 
 
 
	preorder(root);
 
 
 
	return 0;
}
struct node * makeNode(int level, int key, int ch) {
	struct node * newnode = malloc(sizeof(struct node));
	newnode->level = level;
	newnode->key = key;
	newnode->ch_key = ch;
	newnode->left = NULL;
	newnode->right = NULL;
	return newnode;
}
 
void insert(struct node *root, int level, int key, int ch)
{
 
 
	if (root->level == level)
	{
		if (root->right == NULL)
		{
			//newnode = makeNode(level,  key, ch);
			root->right = makeNode(level, key, ch);
			return;
		}
		else //root->right  !=  NULL
			insert(root->right, level, key, ch);//recursion
	}
	else //root->level  !=  newnode->level
	{
		if (root->ch_key == key)
		{
			//newnode = makeNode(level, key, ch);
			root->left = makeNode(level, key, ch);
			return;
		}
		else //root->ch_key  !=  newnode->key
		{
			if (root->left == NULL)
			{
				insert(root->right, level, key, ch);//recursion
			}
			else   // root !=NULL
			{
				insert(root->left, level, key, ch);//recursion
			}
		}
 
	}
 
 
}
 
 
void preorder(struct node *root) {
 
	if (root!=NULL)
	{
		printf("%d\n", root->key);
		preorder(root->left);
		preorder(root->right);
	}
}

이런 코드인데요

제너럴 트리정보를 받아서 바이너리 트리로 바꾸는 겁니다

입력은 (tree level, key, childkey) 형식이구요

이거를 leftmost child right sibling으로 바이너리로 바꾼걸 트리로 바꿔서 출력하는건데

마지막에 0xCDCDCDCD 에러가 나는데 찾아보니까 메모리 누수로 인한 에러인것 같네요

근데 아무리 봐도 누수가 일어날 곳이 없는것 같은데 혹시 아시는분 계신가요?

 의 이미지

root node, 그러니까 main 함수 시작할 때 할당한 node는 left와 right 포인터가 초기화되지 않는군요.

나중에 운 좋게 insert에서 덮어씌워지면 다행이지만, 그렇게 안 될 때는 NULL도 아니고 유효한 노드도 아닌 이상한 포인터가 되겠죠.

그 밖에도 몇 가지 문제점이 있어 보이네요. 다 찾아서 고쳐 드리기는 어렵겠습니다.
ex) main함수의 조건문 if (child == "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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.