首页 > 解决方案 > 为什么 void sort(int *[], int) 会导致“无法读取内存”?

问题描述

我正在开发一个使用动态分配内存的 C++ 程序。该功能int *getNumbers(int)工作正常。

我现在要做的是获取这些信息,然后对其进行排序。当我将它发送给void sorting(int *[], int)我时,我没有收到任何错误消息,而是“无法读取内存”。这发生在:

if (*(sort[index]) < *minElem)   // Around line 122

我对排序并不陌生,但对指针的使用并不陌生。为什么它不能正确运行?

#include <iostream>             //preprocessor directive Pg. 28 and Ch. 1
#include <string>               // Same
#include <cmath>
#include <iomanip>
#include <ctime>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <vector>

using namespace std;

int *getNumbers (int);
void sorting (int *[], int);    //function that I am looking into
                                //does not work

int main ()
{
    int *numbers_2 = nullptr;
    int *numbers = nullptr;     //pointer that I use
    int num;

    cout << "How mamy numbers\t";
    cin >> num;

    while (num < 5 || num > 21) {
        cout << "\nPlease try again - between 5 and 20\n";
        cout << "How mamy num\t";
        cin >> num;
    }

    numbers = getNumbers (num);

    cout << "\nThe numbers are:\n";
    for (int index = 0; index < num; index++) {
        cout << numbers[index] << " ";
    }

    sorting (&numbers, num);    //sorting function does not work

    cout << "\nLet's try this again\n";
    cout << "\nThe numbers are:\n";
    for (int index = 0; index < num; index++) {
        cout << numbers[index] << " ";
    }

    delete[] numbers;
    delete[] numbers_2;
    numbers = nullptr;
    numbers_2 = nullptr;
    cout << "\nFinally Done!!!";
    cout << "\n\n";
    system ("PAUSE");

    return 0;
}

int *getNumbers (int num)
{
    int *array_Num = nullptr;

    array_Num = new int[num];
    for (int index = 0; index < num; index++) {
        cout << "Please enter " << index + 1 << " number\t";
        cin >> array_Num[index];
    }
    return array_Num;
}

void sorting (int *sort[], int size)
{
    int startScan, minIndex;
    int *minElem;

    for (startScan = 0; startScan < (size - 1); startScan++) {
        minIndex = startScan;
        minElem = sort[startScan];
        for (int index = (startScan + 1); index < size; index++) {
            if (*(sort[index]) < *minElem)  //part that I had problems
            {
                minElem = sort[index];
                minIndex = index;
            }
        }
        sort[minIndex] = sort[startScan];
        sort[startScan] = minElem;
    }
}

标签: c++sortingpointers

解决方案


您的问题是您将指向数组的指针(指向 int 的指针)传递到sorting您应该简单地传递数组本身的位置。例如删除'&'你调用的sortinginmain()并将sorting函数更改为:

void sorting (int *sort, int size)
{
    int startScan, minIndex;
    int minElem;

    for (startScan = 0; startScan < (size - 1); startScan++) {
        minIndex = startScan;
        minElem = sort[startScan];
        for (int index = (startScan + 1); index < size; index++) {
            if (sort[index] < minElem)  //part that I had problems
            {
                minElem = sort[index];
                minIndex = index;
            }
        }
        sort[minIndex] = sort[startScan];
        sort[startScan] = minElem;
    }
}

示例使用/输出

$ ./bin/array_sorting
How mamy numbers        4

Please try again - between 5 and 20
How mamy num    6
Please enter 1 number   9
Please enter 2 number   2
Please enter 3 number   12
Please enter 4 number   5
Please enter 5 number   1
Please enter 6 number   3

The numbers are:
9 2 12 5 1 3
Let's try this again

The numbers are:
1 2 3 5 9 12
Finally Done!!!

推荐阅读