传递具有访问冲突的数组和指针
Passing arrays and pointers with access violation
我正在做一个作业,它必须为所有函数传递指针
参数。除全局常量外,不允许使用任何全局变量。
我要在 main 中创建一个 "bids" 的数组,并用 readBids() 函数填充它。这行得通,但我应该将它传递给一个函数来对它进行冒泡排序。一旦我的 sortBids 函数被调用,我的程序就会中断。我现在正在学习指针,我看不出我做错了什么。调用堆栈给出 Project4.exe!main()Line32
,它指向 sortBids(bidArray, numBids);
任何帮助和解释将不胜感激。
#include <iostream>
#include <string>
using namespace std;
string* readProductName();
int* readNumBids();
double* readBids(string,int);
void sortBids(double*, int*);
void averageBid();
void maxBid();
void totalBid();
void printReport();
int main(){
string* productName;
int* numBids;
productName = readProductName();
numBids = readNumBids();
double* bidArray = readBids(*productName, *numBids);
sortBids(bidArray, numBids);
cout << *productName << " " << *numBids << endl;
for (int i = 0; i < *numBids; i++){
cout << bidArray[i] << endl;
}
system("PAUSE");
delete productName;
delete numBids;
delete bidArray;
return 0;
}
string* readProductName(){
string* productName = new string;
cout << "\n Please enter a product name\n";
cin >> *productName;
return productName;
}
int* readNumBids(){
int* numBids = new int;
cout << "\n Please enter the number of bids\n";
cin >> *numBids;
return numBids;
}
double* readBids(string productName, int numBids){
int* size = new int;
size = &numBids;
string* productNamePtr = new string;
productNamePtr = &productName;
double *bidArray;
bidArray = new double[*size];
cout << "\nHow many bids for the " << *productNamePtr << endl;
for (int i = 0; i < *size; i++){
cout << "Please enter bid #" << i + 1 << endl;
cin >> bidArray[i];
if (bidArray[i] <= 0){
cout << "\nPlease enter an amount larger than 0\n";
i--;
}
}
return bidArray;
}
void sortBids(double* array, int *size){
bool* swap = bool{ false };
double* temp = new double;
do
{
*swap = false;
for (int count = 0; count < *size - 1; count++)
{
if (array[count] > array[count + 1])
{
*temp = array[count];
array[count] = array[count + 1];
array[count + 1] = *temp;
*swap = true;
}
}
} while (*swap);
}
问题:
你将 swap
初始化为 0。因为 swap
是指向 bool
的指针,所以你有一个空指针。
您稍后取消引用此指针,而没有让它指向有效的 bool 对象:
*swap = true;
这就是 UB,这就是您遇到访问冲突的原因!
解决方案
要么将此变量定义为普通对象 bool swap = false;
并在任何地方使用交换。或者你正确地初始化它 bool *swap = new bool{false};
并且你到处都使用 *swap
。
杂项咨询:
注意:bidArray 是用 new[]
分配的,因此您必须 delete[]
它或冒未定义行为的风险!
在指针定义中,养成将星号放在变量旁边而不是类型旁边的习惯。为什么 ?因为在视觉上它很混乱:
bool* a,b; // defines a pointer to bool a, but a PLAIN BOOL b !
bool *a,b; // invites otpically to right interpretation by human reader
我正在做一个作业,它必须为所有函数传递指针 参数。除全局常量外,不允许使用任何全局变量。
我要在 main 中创建一个 "bids" 的数组,并用 readBids() 函数填充它。这行得通,但我应该将它传递给一个函数来对它进行冒泡排序。一旦我的 sortBids 函数被调用,我的程序就会中断。我现在正在学习指针,我看不出我做错了什么。调用堆栈给出 Project4.exe!main()Line32
,它指向 sortBids(bidArray, numBids);
任何帮助和解释将不胜感激。
#include <iostream>
#include <string>
using namespace std;
string* readProductName();
int* readNumBids();
double* readBids(string,int);
void sortBids(double*, int*);
void averageBid();
void maxBid();
void totalBid();
void printReport();
int main(){
string* productName;
int* numBids;
productName = readProductName();
numBids = readNumBids();
double* bidArray = readBids(*productName, *numBids);
sortBids(bidArray, numBids);
cout << *productName << " " << *numBids << endl;
for (int i = 0; i < *numBids; i++){
cout << bidArray[i] << endl;
}
system("PAUSE");
delete productName;
delete numBids;
delete bidArray;
return 0;
}
string* readProductName(){
string* productName = new string;
cout << "\n Please enter a product name\n";
cin >> *productName;
return productName;
}
int* readNumBids(){
int* numBids = new int;
cout << "\n Please enter the number of bids\n";
cin >> *numBids;
return numBids;
}
double* readBids(string productName, int numBids){
int* size = new int;
size = &numBids;
string* productNamePtr = new string;
productNamePtr = &productName;
double *bidArray;
bidArray = new double[*size];
cout << "\nHow many bids for the " << *productNamePtr << endl;
for (int i = 0; i < *size; i++){
cout << "Please enter bid #" << i + 1 << endl;
cin >> bidArray[i];
if (bidArray[i] <= 0){
cout << "\nPlease enter an amount larger than 0\n";
i--;
}
}
return bidArray;
}
void sortBids(double* array, int *size){
bool* swap = bool{ false };
double* temp = new double;
do
{
*swap = false;
for (int count = 0; count < *size - 1; count++)
{
if (array[count] > array[count + 1])
{
*temp = array[count];
array[count] = array[count + 1];
array[count + 1] = *temp;
*swap = true;
}
}
} while (*swap);
}
问题:
你将 swap
初始化为 0。因为 swap
是指向 bool
的指针,所以你有一个空指针。
您稍后取消引用此指针,而没有让它指向有效的 bool 对象:
*swap = true;
这就是 UB,这就是您遇到访问冲突的原因!
解决方案
要么将此变量定义为普通对象 bool swap = false;
并在任何地方使用交换。或者你正确地初始化它 bool *swap = new bool{false};
并且你到处都使用 *swap
。
杂项咨询:
注意:bidArray 是用 new[]
分配的,因此您必须 delete[]
它或冒未定义行为的风险!
在指针定义中,养成将星号放在变量旁边而不是类型旁边的习惯。为什么 ?因为在视觉上它很混乱:
bool* a,b; // defines a pointer to bool a, but a PLAIN BOOL b !
bool *a,b; // invites otpically to right interpretation by human reader