[英]Lists in C language. Access violation
this is a bit tricky to explain but I'll try anyway. 解释起来有点棘手,但我还是会尝试的。 I'm trying to create a program that will get a list that ends with 0, and also has 0s in the middle.The program will check who is the minimum value between each zero (for example: for the list 6 -> 3 -> 15 -> 0 -> 1 ->2 ->0
, the minimum values are 3 and 1) , will delete them from the list, and insert them to another list. 我正在尝试创建一个程序,该程序将得到一个以0结尾的列表,中间也有0.该程序将检查谁是每个零之间的最小值(例如:对于列表6 -> 3 -> 15 -> 0 -> 1 ->2 ->0
,最小值为3和1),将从列表中将其删除,并将其插入到另一个列表中。
For example, if list1 is 6 -> 3 -> 15 -> 0 -> 1 ->2 ->0
, then after I run the program, list1 will be 6 -> 15 -> 0 -> 2 ->0
and list2 will be 3->1
. 例如,如果list1为6 -> 3 -> 15 -> 0 -> 1 ->2 ->0
,则在我运行程序后,list1将为6 -> 15 -> 0 -> 2 ->0
, list2将是3->1
。
When I run my code, I get an error of access violation. 运行代码时,出现访问冲突错误。 Here's my code: 这是我的代码:
list* essay(list* anchor1)
{
list* prev_to_min,*runner,*prev_to_runner,*result,*result_temp;
int min;
prev_to_min=prev_to_runner=anchor1;
result=allocate_list();
result_temp=result;
runner=prev_to_runner->address_to_next;
min=runner->number;
while(runner!=NULL)
{
while(runner->number!=0)
{
if(min>=runner->number)
{
min=runner->number;
prev_to_min=prev_to_runner;
}
prev_to_runner=runner;
runner=runner->address_to_next;
}
remove_item(prev_to_min);
result_temp=insert_item(result_temp,min);
prev_to_runner=runner;
runner=runner->address_to_next;
if(runner!=NULL)
min=runner->number;
}
return result;
}
A small explanation since there are so many variables around: result is the pointer to the anchor of list2 (the list of minimums), result_temp is the pointer to the current last item of list2, runner is the pointer which im using to iterate over list1, prev_to_runner is what points to the item before runner in the list, and prev_to_min is what points to the item before a minimum in list1. 一个小小的解释,因为周围有很多变量:result是指向list2的锚点(最小值列表)的指针,result_temp是指向list2的当前最后一项的指针,Runner是im用来迭代list1的指针,则prev_to_runner是指向列表中的跑步者之前的项目,而prev_to_min是指向list1中的最小值之前的项目。 for example 6 -> 3 -> 15 -> 0
, 3 is the minimum, so prev_to_min is the address of 6. 例如6 -> 3 -> 15 -> 0
,最小值是3,所以prev_to_min是地址6。
I tried to run it with a piece of paper, run the program in my head, and I get the needed result. 我试图用一张纸运行它,在脑海中运行该程序,然后得到所需的结果。 but when I compile it and the computer runs it, I get the error "Unhandled exception at 0x5557700c (msvcr100d.dll) in more lists.exe: 0xC0000005: Access violation reading location 0xfffffffc." 但是当我对其进行编译并在计算机上运行它时,出现错误“更多list.exe中的0x5557700c(msvcr100d.dll)未处理的异常:0xC0000005:读取位置0xfffffffc的访问冲突。”
This is the code for inserting and item and deleting an item: 这是用于插入和删除项目的代码:
void remove_item(list* prev_position)
{
list* deleted;
deleted=prev_position->address_to_next;
prev_position->address_to_next=deleted->address_to_next;
free(*deleted);
}
list* insert_item(list* position,listdata x)
{
list* temp=(list*)malloc(sizeof(list));
temp->number=x;
temp->address_to_next=position->address_to_next;
position->address_to_next=temp;
return temp;
}
Access violation reading location 0xfffffffc.
That's a pretty good clue that you had a NULL pointer that you backuped up by the size of a 32 bit integer and then tried to read it. 这是一个很好的线索,您有一个NULL指针,您将其备份了32位整数的大小,然后尝试读取它。
If you run in the debugger, it will tell you where. 如果您在调试器中运行,它将告诉您在哪里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.