简体   繁体   English

导致访问冲突错误的阵列条目删除

[英]Array Entry Removal Causing an Access Violation Error

I have been working on an assignment for university the past few hours and I am pretty much finished save for one error. 在过去的几个小时中,我一直在为大学做作业,除一个错误外,我几乎完成了任务。 I managed to track down the error to a loop inside this function. 我设法将错误追查到该函数内部的循环中。 The function is supposed to remove a desired entry from the call_DB array, then shift the following entries up one index number, then subtract the count by one so that no empty space is left in the array which is later printed into a text file. 该函数应该从call_DB数组中删除所需的条目,然后将后面的条目上移一个索引号,然后将计数减一,以使数组中不留任何空白,然后将其打印到文本文件中。 That function is here: 该功能在这里:

void remove_DB(call_record call_DB[], int & count, string cell_number)
{

   int location = search_DB(call_DB, count, cell_number);

   while (location != -1)
   {
        location = search_DB(call_DB, count, cell_number);

        if (location != -1)
        {
          for (int i = location; i < count; i++)
          {
            call_DB[i] = call_DB[i + 1];
          }

          count--;
        }

   }
 }

In case it is relevant the search function looks through a user defined class array (sorry if that is the wrong terminology) and returns the index 'i' of the location for the searched entry. 如果相关,搜索功能将通过用户定义的类数组进行查找(很抱歉,如果这是错误的术语),并返回所搜索条目的位置索引“ i”。 It returns a -1 if nothing is found. 如果什么也没找到,它将返回-1。 That code is here: 该代码在这里:

int search_DB(call_record call_DB[], int count, string cell_number)
{

 int i;

   for (i = 0; i < count; i++)
   {

      if (call_DB[i].cell_num == cell_number)
      {
        return i;
      }
      return -1;
   }

}

Specifically, the error occurs after the for loop exits when it hits the 'count - -;' 具体来说,该错误在for循环退出时达到“计数--;”时发生。 statement inside remove_DB. remove_DB内的语句。 The last time the loop executes before the error the value for call_DB[i +1] becomes: 上一次在错误发生之前执行循环的时间,call_DB [i +1]的值变为:

"call_DB[i+1] {cell_num= relays=1 minutes=7116664 ...}" “ call_DB [i + 1] {cell_num =中继= 1分钟= 7116664 ...}”

according to the value monitor in Visual Studio. 根据Visual Studio中的值监视器。 If the code proceeds after this iteration a pop-up error which displays the message: 如果代码在此迭代之后继续执行,则会显示一个弹出错误,显示以下消息:

"Unhandled exception at 0x0F6131CA (msvcr120d.dll) in my_program.exe: 0xC0000005: Access violation reading location 0x68FD7339." “ my_program.exe中0x0F6131CA(msvcr120d.dll)处未处理的异常:0xC0000005:访问冲突读取位置0x68FD7339。

Followed by a tab opening in Visual Studio which tells me memcpy.asm is not found. 随后在Visual Studio中打开一个标签页,告诉我找不到memcpy.asm。

I hope this isn't something obvious I'm not seeing but I'm really stumped here so any help would be appreciated. 我希望这不是我看不到的东西,但是我真的很困惑,所以能提供任何帮助。

If i == count-1 , and DB is an array that has count entries, then DB[i + 1] is out of bounds: 如果i == count-1 ,并且DB是具有count条目的数组,则DB[i + 1]超出范围:

for (int i = location; i < count; i++)
{
  call_DB[i] = call_DB[i + 1];
}

Should be: 应该:

for (int i = location; i < count-1; i++)
  call_DB[i] = call_DB[i + 1];

Second, your loop that searches is wrong. 其次,您的搜索循环是错误的。 If the item isn't at position 0 in the array, you will always return -1. 如果该项不在数组中的位置0,则您将始终返回-1。

It should be: 它应该是:

   for (int i = 0; i < count; i++)
   {
        if (call_DB[i].cell_num == cell_number)
        return i;
   }
   return -1;

But you have another issue, and that is you'll have trouble deleting the last item in DB if DB consists of purely data, and not a dummy or sentinel value at the end of the array. 但是,您还有另一个问题,那就是如果DB仅由数据组成,而不是由数组末尾的哑数值或前哨值组成,则您将很难删除DB中的最后一项。 The easiest fix for that would be to declare your array 1 entry larger than what you expect, and have the last entry serve as a dummy value. 最简单的解决方法是声明数组1的条目大于您的期望值,并让最后一个条目用作虚拟值。

Basically, all you need to do, is find the index of the data you want to remove and move everything below that one slot up, thus overwriting the data you want to delete. 基本上,您所需要做的就是找到要删除的数据的索引,然后将所有内容移至该插槽下方,从而覆盖要删除的数据。

void remove_DB(call_record call_DB[], int & count, string cell_number)
{

   int location = search_DB(call_DB, count, cell_number);

   if (location < 0)
      return;

   for (location; location < count - 1; location++)
   {
       call_DB[location] = call_DB[location+1];
   }
   count--;
   return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM