【发布时间】:2015-05-04 18:49:01
【问题描述】:
当我运行我的代码时,指针会损坏
主要
#include <iomanip>
#include <iostream>
#include "Bucket.h"
using namespace std;
int main()
{
const int numberCount = 11;
int startingNumbers[numberCount] = { 100, 79, 255, 355, 70, 85, 10, 5, 47, 62, 75 };
cout << "Initial Numbers" << endl;
for (int count = 0; count < numberCount; count++)
{
cout << startingNumbers[count] << " ";
}
cout << endl;
bucketSort(startingNumbers, numberCount);
system("pause");
}
Bucket.h
void bucketSort (int startingNumbers[], int numberCount);
bucket.cpp
#include <iostream>
#include "Bucket.h"
using namespace std;
void bucketSort(int startingNumbers[], int numberCount)
{
const int cols = 10;
const int rows = 11;
int row = 0;
int input = 0;
int *finger[cols];
int table[rows][cols];
for (int count = 0; count < numberCount; count++)
{
row = startingNumbers[count] % 10;
table[row][count] = startingNumbers[count];
finger[count] = &table[row][count];
}
for (int count = 0; count < numberCount; count++)
{
for (int col = 0; col < cols + 1; col++)
{
if (table[count][col] == *finger[col])
{
startingNumbers[input] = *finger[col];
input++;
}
}
}
for (int count = 0; count < numberCount; count++)
{
cout << startingNumbers[count] << endl;
}
}
根据Visual Studio,直到最后它都不会损坏,我是不是用错了指针?
【问题讨论】:
-
这里
input ++;可以大于数组的大小。您覆盖了数组的末尾,破坏了其他变量的值。 -
我会从标签中删除 c++14。
-
@Evan,你的代码有很多错误,我不明白你在做什么。
-
@Mustafa 很抱歉在我的 C++ 的第一年,只是我们分配的一个项目,我们需要做一个桶排序来重新组织它。
-
@Evan 你可以使用我的指针。:)
标签: c++ visual-c++