사용자로부터 목록을 가져 오는 간단한 프로그램을 작성하려고합니다 (목록은 데이터가있는 구조체이며 다음 목록에 대한 포인터 임). 그런 다음 인쇄하십시오 . 코드가 제대로 작동하지만 인쇄 후 "처리 4.exe에서 0x011e1502의 처리되지 않은 예외 : 0xC0000005 : 0xCdcdcdcd 위치를 읽는 액세스 위반"오류가 발생합니다.C에서 처리되지 않은 예외로 처음으로 작업하는 경우
아무도 말해 줄 수 있습니까? 여기 내 코드 :
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef int list_data;
typedef struct list
{
list_data number;
struct list* next_list;
} list; //definition of a list
list* create_list()
{
list* anchor;
anchor=(list*)malloc(sizeof(list));
anchor->next_list=NULL;
return anchor; // allocates a memory for a list and returns address of first block
}
list* insert_list(list* current_position,list_data x)
{
list* temp;
temp=(list*)malloc(sizeof(list));
temp->number=x;
temp->next_list=current_position->next_list;
current_position->next_list=temp;
return temp; //inserts a new block with a data of x
}
void printlist(list* anchor)
{
list* current_list=anchor->next_list;
while(current_list!=NULL)
{
printf("%3d -> ",current_list->number);
current_list=current_list->next_list;
}
printf("End\n");
}
void scan_list(list* anchor)
{
int num1=1;
list* current_position=anchor;
printf("Enter values until 0\n");
while(num1!=0)
{
scanf("%d",&num1);
if(num1)
current_position=insert_list(current_position,num1);
}
}
void main()
{
list* anchor;
anchor=create_list();
scan_list(anchor);
printf("\n");
printlist(anchor);
free(anchor);
getch();
}
나를 위해 잘 작동합니다. 너 어떻게 지내니? – nommyravian
''anchor' = 메모리 누수 만 자유롭게합니다. 또한 명시 적으로 어느 행 **가 해당 액세스 위반 오류를 표시하는지 ** 알 수 있으면 좋을 것입니다. –
printf ("% 3d ->", current_list-> number); 나에게 오류를 줬지만 지금은 효과가있는 것 같다. 간단한 재부팅으로 문제가 해결 된 것으로 보입니다. 시간을 낭비해서 죄송합니다. –