我是一名初学者C ++学生,我遇到了一个恼人的错误。我的程序让我得到了所需的输出但仍然崩溃。
当我运行程序时,我收到错误:运行时检查失败#2 - 堆叠变量' pointerarray'被腐败了。 如果我忽略了我得到错误:运行时检查失败#2 - 围绕变量' sortarray'被腐败了。这是在VS 2012中。当我单击重试进行调试并查看错误发生的位置时,它会将我带到函数的末尾,然后再返回main。
这是我的代码:
Header.h :
void bucketsort (int inputarray[], int); //Sort function that gets passed an array and has return type double to return array values.
void randomarray (int[], int); //Takes in array and size to generate random numbers
int main(){
/*const int size = 16;*/
const int size = 11;
// int bucketarray[size -1]; //allocates memory for array 0-15, 16 elements
/*randomarray(bucketarray, size);*/
int bucketarray[size] = {100, 79, 255, 355, 70, 85, 10, 5, 47, 62, 75};
//Print array
cout << "The orgional numbers are" << endl;
for ( int i=0 ; i < size; i++) {
cout << bucketarray[i] << endl;
}//end of for
bucketsort(bucketarray,size);
cout << "The sorted numbers are" << endl;
for ( int i=0 ; i < size; i++) {
cout << bucketarray[i] << endl;
}//end of for
}
void bucketsort (int inputarray[],int count) {
//Generating a two dimensional aray to sort the numbers.
const int ROWS = 9; //10 0-9
const int COLS = 11; //16 0-15 cant set equal to count or error
//Pointer vars couldn't get structs to work
int *pointerarray [COLS];
int sortarray [ROWS][COLS];
//Array for all mods
int modarray[] = {1,10, 100, 1000};
int digitarray[COLS]; //Stores amount of digits each number has
int i = 0;
while( i < count) {
int digits = 0;
int x = inputarray[i];
x /= 10;
while(x > 0)
{
x = x/10;
digits++;
}//end x white
digitarray[i] = digits + 1; //I was always 1 under didn't wanna think, added 1.
i++;
}//end i while
//Finds largest amount of digits in the array
int largestdigit;
largestdigit = digitarray[0];
for (int i = 1; i < count; i++)
{
if (digitarray[i] > largestdigit)
largestdigit = digitarray[i];
}// end of for
//Print array
cout << "The amount of digits are" << endl;
for ( int i=0 ; i < count; i++) {
cout << digitarray[i] << endl;
}//end of for
////Uses largest number to see how many times it must be passed based on the number of digits.
int x = 0;
while(x < largestdigit) {
//Pass Code
int i = 0;
int row = 0;
int inputnum = 0; //reset counting var
while (i < count) {
row = (inputarray[i]/modarray[x]) % 10; //Divides by 1, 10, 100, 1000
sortarray[row][i] = inputarray[i];
pointerarray[i] = &sortarray[row][i]; //This var can be used to plug them back into origonal array
i ++ ; //Increments to next input from list
} // end of while.
//This needs to return row by row
i = 0;
inputnum = 0;
while( i < count - 1){
//All items in row 0
for (int c = 0; c < COLS; c++) { //c is used for all col values
if (sortarray[i][c] == *pointerarray[c]) { //Pointer stores 2d array values and inputs them to the list
inputarray[inputnum] = *pointerarray[c];
inputnum ++;
}//end if
}//end for
i ++ ;
} // end while
//clear the array for next pass
i = 0;
while( i < count - 1){
//All items in row 0
for (int c = 0; c < COLS; c++) { //c is used for all col values
sortarray[i][c] = 0; //Clears array for next pass
}//end for
i ++ ;
} // end while
x++;
} // end of pass amount while
}//end of function bucketsort
在函数bucketsort结束时,我收到错误并且崩溃了。关于此错误的所有其他帖子都是将值分配给从未初始化的数组值。就像有数组[5]是数组[0-4]并将某些东西分配给数组[5],但我无法在我的代码中找到它。我认为这与我的pointerarray搞砸了有关,因为我通过将其所有值设置为0来清除我的2d数组。我已经被困了一段时间,请帮助。