c++ - 为什么 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;
}
}
解决方案
您的问题是您将指向数组的指针(指向 int 的指针)传递到sorting
您应该简单地传递数组本身的位置。例如删除'&'
你调用的sorting
inmain()
并将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!!!
推荐阅读
- javascript - 使用汇总将 TypeScript 构建到 JavaScript 时如何删除注释
- php - Laravel 符号链接 403
- atom-editor - 安装“platformio-ide-terminal@2.10.0”失败
- java - gcloud 数据存储使用 UUID 的密钥
- python - 如何格式化二次方程输出?
- elasticsearch - 弹性搜索 - 加入最佳实践
- javascript - 使用两个 where 条件运行 sequelize
- javascript - 比较两个数组并填充缺失值
- ruby-on-rails - 如何在中使用 image_tag
- python - 使用键参数按多列对 Pandas 数据框进行排序