gpt4 book ai didi

c++ - 为什么 void sorting(int *[], int) 会导致 "Unable to read memory"?

转载 作者:行者123 更新时间:2023-11-30 03:18:16 25 4
gpt4 key购买 nike

我正在开发一个使用动态分配内存的 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;
}
}

最佳答案

您的问题是您将指向数组的指针(指向指向int 的指针的指针)传递给排序,而您应该只传递数组本身。例如。在 main() 中删除调用 sorting'&' 并将 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!!!

关于c++ - 为什么 void sorting(int *[], int) 会导致 "Unable to read memory"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54838233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com