처리되지 않은 예외가 throw됨: 읽기 액세스 위반입니다. top이(가) nullptr였습니다.
조회수 7699회
제목과 같은 오류가 뜨는데 무슨 문제인가요? 그대로 보고 따라하것인데 잘 모르겠네요
#include <stdio.h>
#include <stdlib.h>
typedef struct list { //키와 주소를 가진 연결리스트 구조체 선언후 node 라고 이름지음
int key;
struct list *next;
} node;
node *top, *bottom;
int init_stack(int ) { //초기화
top = (node*)malloc(sizeof(node));
bottom = (node*)malloc(sizeof(node));
top -> next = bottom;
bottom -> next = bottom;
return 0;
}
int print() { //출력
node *p;
p = top->next;
while (p != bottom) {
printf("%d ", p->key);
p = p->next;
}
puts("");
return 0;
}
int push(int n) { //push
node *p;
if ((p = (node*)malloc(sizeof(node))) == NULL) {
printf("메모리 초과\n");
return -1;
}
printf("push %d ", n);
p-> key = n;
p->next = top ->next; //p가 스택 맨 위가 됨
top -> next = p; //p를 최상위 다음에 push
print();
return n;
}
int pop() {//pop
node *p; //삭제할 임시노드
int n; //삭제 후 삭제된 정수 값 return용
if (top->next == bottom) { //스택 비었나 확인
printf("스택에 아무것도 없음\n");
return -1;
}
p = top->next; //top_next 노드 삭제를 위해 p에 저장
n = p->key; //삭제하려는 p의 키 값 저장
printf("pop %d ", n);
top->next = p -> next; //head->next 노드가 삭제하려는 노드의 다음 노드를 가리키도록 함
free(p); //p 삭제
print();
return n;
}
int clear() { //스택 비우기
node *p, *q;
p = top->next; //첫 노드를 p에 저장
while (p != bottom) {
q = p; //노드를 삭제하기 위해 q에 저장
p = p -> next; //p는 다음 노드로 건너감
free(q); //저장한 노드 삭제
} //저장한 노드 삭제 안하면 메모리 낭비 발생
top->next = bottom;
printf("스택 비움\n");
return 0;
}
int main() {
push(1);
push(2);
pop();
push(3);
clear();
pop();
return 0;
}
-
(•́ ✖ •̀)
알 수 없는 사용자
2 답변
-
초기화-clear() 후에는 반환 작업-pop() 이 불가능합니다.
아무것도 없기 때문이죠.
null 포인트(빈 값)에 접근했기 때문에 에러가 발생합니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
-
-
2가지 오류가 있습니다.
- init_stack 함수의 매개변수가 잘못되었습니다.
- main 함수에서 init_stack 함수를 사용하지 않았습니다. 스택을 사용하기 전에 가장먼저 init_stack 함수를 한번 사용해야 합니다.
아래 코드 참고하세요. init_stack 함수의 매개변수 부분을 수정하고, main 함수에 init_stack 함수 호출을 추가했습니다. 그리고 가독성을 높이려고 print 함수의 윗부분에
puts("");
를 한줄 추가했습니다. 나머진 원래 코드 그대로입니다.- 코드
#include <stdio.h> #include <stdlib.h> typedef struct list { //키와 주소를 가진 연결리스트 구조체 선언후 node 라고 이름지음 int key; struct list* next; } node; node* top, * bottom; int init_stack() { //초기화 top = (node*)malloc(sizeof(node)); bottom = (node*)malloc(sizeof(node)); top->next = bottom; bottom->next = bottom; return 0; } int print() { //출력 puts(""); node* p; p = top->next; while (p != bottom) { printf("%d ", p->key); p = p->next; } puts(""); return 0; } int push(int n) { //push node* p; if ((p = (node*)malloc(sizeof(node))) == NULL) { printf("메모리 초과\n"); return -1; } printf("push %d ", n); p->key = n; p->next = top->next; //p가 스택 맨 위가 됨 top->next = p; //p를 최상위 다음에 push print(); return n; } int pop() {//pop node* p; //삭제할 임시노드 int n; //삭제 후 삭제된 정수 값 return용 if (top->next == bottom) { //스택 비었나 확인 printf("스택에 아무것도 없음\n"); return -1; } p = top->next; //top_next 노드 삭제를 위해 p에 저장 n = p->key; //삭제하려는 p의 키 값 저장 printf("pop %d ", n); top->next = p->next; //head->next 노드가 삭제하려는 노드의 다음 노드를 가리키도록 함 free(p); //p 삭제 print(); return n; } int clear() { //스택 비우기 node* p, * q; p = top->next; //첫 노드를 p에 저장 while (p != bottom) { q = p; //노드를 삭제하기 위해 q에 저장 p = p->next; //p는 다음 노드로 건너감 free(q); //저장한 노드 삭제 } //저장한 노드 삭제 안하면 메모리 낭비 발생 top->next = bottom; printf("스택 비움\n"); return 0; } int main() { init_stack(); push(1); push(2); pop(); push(3); clear(); pop(); return 0; }
- 결과
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력