我在课堂上遇到的课程遇到了麻烦,甚至老师也找不到问题。我们正在做一个程序,要求用户输入double然后当他停止时,它会扫描数组并将正面和负面分开,将它们放在不同的数组中。
我们注意到当我们使用浮动时,程序工作更多的数字,但如果我们输入太多仍然是错误,如果我们使用双重错误后只有几个数字。我的意思是错误,程序运行良好但是当它显示结果时,数组中有一些奇怪的数字。这是使用double的代码:
#include <iostream>
using namespace std;
void filling(double *, int &);
void sortPositiveNegative(double *, double *, double *, int, int &, int &);
void display(const double *, int);
int main () {
double * vecteur = new double;
double * positive = new double;
double * negative = new double;
int counter = 0, counterPos = 0, counterNeg = 0;
cout << "Filling of the real number vector " << endl;
filling(vecteur, counter);
cout << endl << "Display of the real number vector " << endl;
display(vecteur, counter);
cout << endl << "Sort of the positive and negative in the real number vector: " << endl;
sortPositiveNegative(vecteur, positive, negative, counter, counterPos, counterNeg);
cout << endl << "Display of the positive real number : " << endl;
display(positive, counterPos);
cout << endl << "Display of the negative real number : " << endl;
display(negative, counterNeg);
system("PAUSE");
return 0;
}
void filling (double *vecteur, int &counter)
{
bool validation;
char choice = 'Y';
do
{
do
{
validation = true;
cout << "Please enter the value of case " << counter+1 << ": ";
cin >> vecteur[counter];
if(cin.fail())
{
cerr << "The number entered is not valid." << endl;
cin.clear();
validation = false;
}
while(cin.get() != '\n'){}
}while(!validation);
counter++;
do
{
validation = true;
cout <<"Do you wish to continue? (Y/N): ";
cin >> choice;
if(toupper(choice) != 'Y' && toupper(choice) != 'N')
{
cerr << "We don't understand your choice, please try again." << endl;
cin.clear();
validation = false;
}
while(cin.get() != '\n'){}
}while(!validation);
}
while(toupper(choice)=='Y');
}
void sortPositiveNegative(double *vecteur, double *positive, double *negative, int counter, int &counterPos, int &counterNeg)
{
int i = 0;
for(i; i<counter;i++)
{
if(vecteur[i] >= 0)
positive[counterPos++] = vecteur[i];
else
negative[counterNeg++] = vecteur[i];
}
}
void display (const double *vecteur, int counter)
{
for(int i = 0; i<counter;i++)
cout << vecteur[i]<<endl;
cout << endl;
}
我的老师认为这可能是一个记忆问题,但我们不知道为什么会这样做。
提前致谢。
答案 0 :(得分:7)
肯定存在内存问题,我不知道如何使用float
来修复它。例如,下面的代码行只分配了一个 double而不是一个双精度数组:
double * vecteur = new double;
然后,您使用此vecteur
,就像它是N个元素的数组一样。这会触发未定义的行为。
要修复它,您必须根据需要分配一个尽可能多的值数组。例如,假设你需要10,那么你就像这样分配10:
double * vecteur = new double[10];
但是,如果您事先不知道元素的数量,则每次要添加元素时都需要扩展数组。如果你用C语言写这个,我建议你使用realloc()
。但是考虑到你使用C ++,只需坚持使用std::vector<double>
,它就会自动管理内存。例如:
#include <vector>
int main()
{
std::vector<double> vecteur; // Use vector to store array of doubles.
// Add as many elements as you want.
// Vector will resize itself if/when needed.
vecteur.push_back(.1);
vecteur.push_back(.2);
vecteur.push_back(.3);
vecteur.push_back(.4);
vecteur.push_back(.5);
}
希望它有所帮助。祝你好运!
答案 1 :(得分:2)
double * vecteur = new double;
您为一个双重
分配空间filling(vecteur, counter);
将其传递给filling
cin >> vecteur[counter];
然后填充直到用户按下你已经为你分配了内存的一个元素的数据
double
vs float
并不重要。 float
只是更小,因此会慢慢破坏内存。但它仍然从vecteur[1]
开始腐蚀记忆
我建议您使用std::vector<dobule>
而不是普通指针,并使用push_back
填充
答案 2 :(得分:2)
double * vecteur = new double;
double * positive = new double;
double * negative = new double;
这里每次只分配一次双倍。您存储在“数组”中的第一个项目很好,但之后的任何其他内容都是未定义的行为。
修复方法是实际分配所需数量的项目:
double * vecteur = new double[MAXIMUM8NUMBER_OF_ITEMS];
double * positive = new double[MAXIMUM8NUMBER_OF_ITEMS];
double * negative = new double[MAXIMUM8NUMBER_OF_ITEMS];
或者更好的是,使用像std::vector
这样的标准容器。