从双重和错误中丢失数据:Windows 在 exe 中触发了断点.这可能是由于堆损坏
Lost data from double and Error: Windows has triggered a breakpoint in exe. This may be due to a corruption of the heap,
我已经处理了一段时间的代码,我几乎让它工作了,但我遇到了一个问题。我在排序时丢失了数据。我输入的数字越多,显示的数字就越多。链接到我的输出图像
同样,一旦代码运行,我就会收到一个带有此消息的弹出窗口(我注意到在添加 sum 函数和之后的每个函数后我收到此错误,我没有按 F12):
"Windows 在 project3.exe 中触发了一个断点。
这可能是由于堆损坏,这表示 project3 或其加载的任何 DLL 中存在错误.exe或它已加载的任何 DLL 中存在错误。
这也可能是由于用户在 F12 时按下 F12项目3.exe有重点。
输出窗口可能包含更多诊断信息。"
这是我的代码:在最后一个编号输入后询问用户编号,他们必须按零,它显示数据,显示一些统计信息并对数据进行排序。我没有检查用户错误
(*如果我不使用system("pause")
终端窗口就会关闭,这就是为什么它在那里,我知道我应该'delete []arr
,但在我放零后它会给我断点消息,这就是为什么它被注释掉)
#include <iostream>
#include <iomanip>
using namespace std;
void getdata(double *arr, double &data, int &floatpt);
void display (double *arr, int floatpt);
void computesum (double *arr, int floatpt, double sum);
void computearthmeticmean (double *arr, int floatpt, double aMean);
void computeharmonicmean (double *arr, int floatpt, double hMean);
void median(double *arr, int floatpt);
void sort (double *arr, int floatpt);
int main ()
{
double data, sum=0, aMean=0, hMean=0;
int count=0, floatpt;
double *arr =new double[count];
getdata (arr, data, floatpt);
cout<<"Thank you. The data you entered are "<<endl;
display(arr, floatpt);
cout<<"The following statistics were computed "<<endl;
computesum(arr,floatpt, sum);
computearthmeticmean(arr, floatpt, aMean);
median(arr, floatpt);
computeharmonicmean (arr, floatpt, hMean);
sort(arr, floatpt);
cout<<"The original data set is "<<endl;
display(arr, floatpt);
cout<<"Thank you for using this program. Enjoy your statistics "<<endl;
//delete []arr;
system ("pause");
return 0;
}
void getdata(double *arr, double &data, int &floatpt)
{
int count=0;
cout<<"Please enter floating point data.n";
cout<<"After the last number has been entered press 0 (zero) n";
do
{
cin>>arr[count];
data = arr[count];
count++;
}while(data != 0);
floatpt=(count-1);
}
void display (double *arr, int floatpt)
{
for(int i=0; i<floatpt; i++)
{
cout<<arr[i]<<endl;
}
}
void computesum (double *arr, int floatpt, double sum)
{
for (int j=0; j<floatpt; j++)
{
sum+=arr[j];
}
cout<<"Sum: "<<sum<<endl;
}
void computearthmeticmean (double *arr, int floatpt, double aMean)
{
for (int a=0; a<floatpt; a++)
{
aMean+=arr[a];
}
aMean=aMean/floatpt;
cout<<"Arithmetic Mean: "<<aMean<<endl;
}
void computeharmonicmean (double *arr, int floatpt, double hMean)
{
for (int h=0; h<floatpt; h++)
{
hMean+=(1/arr[h]);
}
hMean=floatpt/hMean;
cout<<"Harmonic Mean: "<<hMean<<endl;
}
void median(double *arr, int floatpt)
{
int temp;
double median;
for (int s=0; s<floatpt; s++)
{
for (int r=0; r<(floatpt-1); ++r)
{
if (arr[r] > arr[r+1])
{
temp = arr[r];
arr[r] = arr[r+1];
arr[r+1] = temp;
}
}
if (floatpt%2 == 0)
{
median = (arr[s/2] + arr[(s/2)-1])/2.0;
}
else
{
median = arr[s/2]/1.0;
}
}
cout<<"Median: "<<median<<endl;
}
void sort (double *arr, int floatpt)
{
cout<<"The sorted data set is: "<<endl;
for (int sd=0; sd<floatpt; sd++)
{
cout<<arr[sd]<<endl;
}
}
int count=0, floatpt;
double *arr =new double[count];
count
为 0,因此您创建的数组没有分配。因此,在getdata
中,您可以读取超出边界的内存位置:
您打算稍后重新分配吗?
也许如果你尝试使用std::vector<double>
,那么事情就会解决。它会自动调整大小,并且很容易进行边界检查。
像这样的东西(未经测试):
#include <vector>
// ...
std::vector<double> data;
getdata(arr);
// ...
void getdata(std::vector<double>& arr)
{
double nextValue;
cout<<"Please enter floating point data.n";
cout<<"After the last number has been entered press 0 (zero) n";
cin>>nextValue;
while(nextValue != 0)
{
arr.push_back(nextValue);
cin >> nextValue;
}
}
相关文章:
- 分段故障(堆芯转储)矢量
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 为什么会发生堆损坏
- 释放错误后堆使用
- 函数中堆分配的效果与缺少堆分配的情况
- c++r值引用应用于函数指针
- 在将变量声明为引用时,堆在释放后使用
- 如果编译的源代码是特定于它编译的硬件的,我们如何分发它
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何仅使用对象名称打印特定于对象的成员
- 在功能块中使用新运算符时存在于堆或堆栈上?
- "new"创建的实例的所有成员变量是否都存在于堆上而不是堆栈上?
- C++17 和更新的 std::分配器是否适用于动态数量的自定义堆?
- 在 C++ 中,映射相对于堆的优势是什么?
- 从双重和错误中丢失数据:Windows 在 exe 中触发了断点.这可能是由于堆损坏
- 由于加载的堆或 dll 损坏,Windows 触发了断点
- 是相对于对象是在堆栈上还是在堆上分配的继承成本
- 设置依赖于调用栈的条件断点
- 为什么范围基本循环不适用于堆中的数组,但适用于堆栈中的数组
- 如何使用 libumem 查找堆损坏,而不依赖于'core'文件?