링크드 리스트를 이용한 동적 큐를 구현한건데.. 실행버그가 있

shs0917의 이미지

#include <stdio.h>
#include <stdlib.h>


#define MAX_QUEUES 10
#define IS_FULL(temp) (!(temp))
#define IS_EMPTY(front) (!(front))

typedef struct _element element;
struct _element{
	int key;
};

typedef struct queue *queue_pointer;
struct queue{
	element item;
	queue_pointer link;
};



void addq(queue_pointer *front, queue_pointer *rear, element item);
element deleteq(queue_pointer *front);

int main(void){
	int i;
	element item[MAX_QUEUES];
	queue_pointer front[MAX_QUEUES] = {NULL}, rear[MAX_QUEUES] = {NULL};
	
	for(i = 0; i < MAX_QUEUES; i++){
		item[i].key = i;
		addq(&front[i], &rear[i], item[i]);
		printf("[%d]rear->item: %d\n", i, rear[i]->item.key);
	}

	printf("\n\n");

	for(i = MAX_QUEUES - 1; i >= 0; i--){
	//i = MAX_QUEUES - 1;
	//while(!IS_EMPTY(front[i])){
		printf("[%d]front->item: %d\n", i, deleteq(&front[i]).key);
		//i--;
	}

	return 0;
}

void addq(queue_pointer *front, queue_pointer *rear, element item){
	queue_pointer temp = (queue_pointer)malloc(sizeof(temp));
	if(IS_FULL(temp)){
		fprintf(stderr, "The memory is full\n");
		exit(1);
	}

	temp->item = item;
	temp->link = NULL;
	if(*front)
		*rear = temp;
	else
		*front = temp;
	*rear =temp;
}

element deleteq(queue_pointer *front){
	queue_pointer temp = *front;
	element item;
	if(IS_EMPTY(*front)){
		fprintf(stderr, "The queue is empty\n");
		exit(1);
	}
	item = temp->item;
	*front = temp->link;
	
	free(temp);

	return item;
}

deleteq에서 free하는게 잘못된거 같은데요.. free를 없애면 에러가
안나거던요.. 컴파일할때는 경고도 하나 안뜨던데.. 그리고.. 옆에서 누가
프리 안해줘도 컴파일러가 알아서 해준다면서 그러던데.. 요즘 C 컴파일러는
free도 알아서 해주나요?? 답변 주시는 분들께 미리 감사 드립니다.
이한길의 이미지

혹시 컴파일러를 확인해보실래요?
전 대략 되는거 같은데.. 에러 없이.. 그래서 이유도 모르겠군요... 이런~
도움이 안되서 죄송합니다.. 코드는 대략 봐서 제대로 모르겠습니다..
어디서 문제가 나는지.. 무슨 문제가 나는지...

----
먼저 알게 된 것을 알려주는 것은 즐거운 일이다!
http://hangulee.springnote.com
http://hangulee.egloos.com

sozu의 이미지

queue_pointer temp = (queue_pointer)malloc(sizeof(temp));

에서 sizeof(temp)를 다음과 같이 바꿔야 하지 않을까요

queue_pointer temp = (queue_pointer)malloc(sizeof(struct queue));

^-^

-----------
청하가 제안하는 소프트웨어 엔지니어로써 재미있게 사는 법
http://sozu.tistory.com

shs0917의 이미지

아.. 제가 잘못 사용했군요.. 감사합니다.. 동적할당은
처음 사용해보는 거라서 잘 몰랐습니다.. 감사합니다.

컴퓨터가 이해할수 있는 코드는 어느 바보나 다 작성할 수 있다. 좋은 프로그래머는 사람이 이해할 수 있는 코드를 짠다 - 마틴파울러

댓글 달기

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