gpt4 book ai didi

c++ - 堆损坏 - 为什么?

转载 作者:行者123 更新时间:2023-11-30 00:53:12 25 4
gpt4 key购买 nike

我有以下代码:

std::vector<double> thickness(61);
std::vector<double> hThickness(61);
std::vector<double> samplesAfterMueller(61);

在我的函数的顶部。运行后,它会因堆损坏而崩溃,我不知道为什么以及如何。

我将其缩小到这些行并将其更改为

std::vector<double> thickness; thickness.resize(61);
std::vector<double> hThickness; hThickness.resize(61);
std::vector<double> samplesAfterMueller; samplesAfterMueller.resize(61);

现在可以了。为什么?我以为std::vector<double> myVector(size);应该在初始化时就已经初始化了一定的大小吗?

注意:在不运行该函数的情况下,我没有任何堆损坏,并且代码没有未释放的内存,所以它真的归结为这些行。

编辑:功能齐全

int PMNFL::CalcMatchesFixedRadius(float* input, DataPair mData, int w, float radius, float& error, float& veneOffset, bool leftEye)
{
double cosinus = cos(2*mData.angle);
double sinus = sin(2*mData.angle);

// Uncommend to fix heap corruption
/*std::vector<double> thickness; thickness.resize(61);
std::vector<double> hThickness; hThickness.resize(61);
std::vector<double> samplesAfterMueller; samplesAfterMueller.resize(61);*/

// Uncomment to cause heap corruption
std::vector<double> thickness(61);
std::vector<double> hThickness(61);
std::vector<double> samplesAfterMueller(61);

float bestHit = 50000.0f;
float last = 60000.0f;
int cVal = 2;

int da = (fabs(mData.deltaAngle))>10?10:(int)(fabs(mData.deltaAngle)+0.5f);

for(int i = 0; i < 61; ++i)
{
samplesAfterMueller.at(i) = 0.0;
double radum = radius * 10.0;
double value = pow(radum,2) - 100.0 * pow(double(30-i), 2); // Pythagoras
if(value < 0)
{
hThickness.at(i) = 0.0;
continue;
}
hThickness.at(i) = sqrt(value) + radum;
}

double amplitude = pow((radius * 10.0), 2) * (2 + PI / 2) / (40 * w);
double frequency = (2*PI / (40*w));
for(int i = 0; i < 61; ++i)
{
if(i > 30 - 2*w && i < 30 + 2*w)
thickness.at(i) = mData.nflThickness + amplitude + amplitude * (-cos(frequency * (2*w - i) * 10));
else
thickness.at(i) = mData.nflThickness;
}

int posR = ((int)(mData.distRad + 2.5f) / 5) * 5;
if(posR < 0)
posR = 0;

if(posR > 245)
posR = 245;

int baseOffset = da * 1269 * 61;
if(posR > 0)
{
baseOffset += (posR / 5 - 1) * 11 * 1269 * 61;
}

const double min_R = 2.0; //the minimum radius
const double step_R = 0.5;//step size of radius;
int iR = (int)floor((radius-min_R)/step_R); //=16th block.

int line = baseOffset + iR*(31+16);

for(int k = 0; k < 16; ++k)
{
int index = line + k*61;
for(int i = 0; i < 61; ++i)
{
double retardance = 0.62 * thickness.at(i);
double retardancePhase = 2*PI*retardance/532.0;
double M11 = pow(cosinus,2) + pow(sinus,2) * cos(retardancePhase);
double Ic = fSamples.at(index+i) * (1-M11)/2;
samplesAfterMueller.at(i) += Ic;
}
}

float* adapted = new float[4*w+1];

for(int i = 0; i < 4*w+1; ++i)
{
adapted[i] = input[4*w-i];
}

std::vector<double> simData(61);

for(int c = 0; c < 30; ++c)
{
for(int i = 0; i < 61; ++i)
{
simData.at(i) = samplesAfterMueller.at(i) + fSamples.at(line+16*61+c*61+i)/2;
}

last = GetSADFloatRel(adapted, &simData.at(30-2*w), 4*w+1, 4*w+1, 0);
if(bestHit > last)
{
bestHit = last;
cVal = c * 2;
}

last = GetSADFloatRel(input, &simData.at(30-2*w), w * 4 + 1, w * 4 + 1, 0);
if(bestHit > last)
{
bestHit = last;
cVal = c * 2;
}
}

error = bestHit;
delete adapted;
return cVal;
}

最佳答案

您为 adapted 使用了错误的 delete,这会调用未定义的行为(您需要使用 delete [])。您应该考虑使用临时 vector 来为您管理内存。

关于c++ - 堆损坏 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17046153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com