Почему цикл for в приведенном ниже коде приводит к ошибке двойного освобождения или повреждения (fasttop)? - PullRequest
0 голосов
/ 25 октября 2019

Эта программа показывает ошибку в заголовке при завершении 3-й итерации (завершается, когда i становится 3). У меня есть засыпки, когда программа завершается, и последняя остановка происходит после последнего i, когда i = 2, и перед первым i, когда i равен 3. Почему итерация из третьего массива в четвертый вызовет двойное освобождение или повреждение?

int main(int argc, char* argv[]) {
  std::ifstream inFile;

  inFile.open(argv[1]);

  if (!inFile.is_open()) {
    std::cout << "File could not be opened" << '\n';

    return 0;
  }

  int n = 0;

  inFile >> n;

  std::cout << n << std::endl;

  for (int i = 0; i < n; i++) {
    std::cout << i << std::endl;
    int m;

    inFile >> m;

    int* arr = new int[m];

    std::cout << "Unsorted list:" << std::endl;
    std::cout << i << std::endl;
    for (int j = 0; j < m; j++) {
      inFile >> arr[j];

      std::cout << arr[j] << ' ';
    }

    std::cout << std::endl;
    std::cout << i << std::endl;
    BubbleSort<int> bubble = BubbleSort<int>(m, arr);
    bubble.sort();

    InsertionSort<int> insert = InsertionSort<int>(m, arr);
    insert.sort();

    SelectionSort<int> select = SelectionSort<int>(m, arr);
    select.sort();

    std::cout << "Sorted Lists:" << '\n';

    std::cout << "Bubble:" << '\n';
    bubble.printArray();

    std::cout << "Insertion: " << '\n';
    insert.printArray();

    std::cout << "Selection: " << '\n';
    select.printArray();

    delete[] arr;
    std::cout << i << std::endl;
  }
  return 0;
}
...