C++ 访问冲突读取位置:指向类构造函数的第一行
好的,我有一个模型,它已经运行了好几天了,突然我得到了一个错误:访问违反读取位置。由于它昨晚运行得“很好”,我只能假设我很幸运,而且未定义的行为在我得到这个模型“运行”后一直对我“有利”。此外,调试器指向我的LengthDistribution对象的构造函数的第一行 起初,我认为有些东西在使用之前就超出了范围,但我改变了LD对象的范围(我认为),错误仍然存在。这是我的主要观点C++ 访问冲突读取位置:指向类构造函数的第一行,c++,C++,好的,我有一个模型,它已经运行了好几天了,突然我得到了一个错误:访问违反读取位置。由于它昨晚运行得“很好”,我只能假设我很幸运,而且未定义的行为在我得到这个模型“运行”后一直对我“有利”。此外,调试器指向我的LengthDistribution对象的构造函数的第一行 起初,我认为有些东西在使用之前就超出了范围,但我改变了LD对象的范围(我认为),错误仍然存在。这是我的主要观点 int main(int argc,char**argv) { int h_最大长度; int h_质量; int h_
int main(int argc,char**argv)
{
int h_最大长度;
int h_质量;
int h_pause=0;
int h_showFutures=0;
int h_=0;
int h_aggModel=0;
int h_runModel=0;
int h_pathwayTrace=0;
int h_showCurrent=0;
int h_numberOfIterations=15;
int h_debugMode=0;
int*初始条件;
int h_=1;
int线程=8;
//表示随机模型初始条件的LD对象
长度分布*LD;
//保存每个模型运行结果的随机模型数组。
随机模型**resultHolder;
std::cout>h_运行模型;
if(h_runModel==1)
{
std::cout>h_迭代次数;
std::cout>h_模型;
标准::cout>h_质量;
标准::cout>h_最大长度;
std::cout>h_;
std::cout>线程;
//初始化结果保持数组。
resultHolder=新的随机模型*[h_hostIterations*线程];
//根据初始质量和所需最大长度创建初始分布。
temp_initialConditions=新整数[h_maxlength];
对于(int i=0;i
}
触发此错误的LD对象的相应构造函数如下所示:
LengthDistribution::LengthDistribution(int* initialDistribution, int maxlength, int debug)
{
m_maxlength = maxlength;
m_debug = debug;
m_nonZeroCount = 0;
m_nonZerosIndicesCrop = new int[m_nonZeroCount];
m_baseStateHolder = new int*[m_nonZeroCount];
m_futureStateCount=0;
m_aggStateCount=0;
m_fragStateCount=0;
m_base=new int[maxlength];
m_myFreeEnergy=0;
m_r=0;
m_s=0;
m_q=0;
m_stateOrigin=0;
m_partitionFunction=1;
m_futureLDHolder= new LengthDistribution*[m_nonZeroCount];
m_futureStateHolder=new int*[m_nonZeroCount];
m_myTransitProb = 0;
m_lengthDistribution = new int[m_maxlength];
memcpy(m_lengthDistribution, initialDistribution, m_maxlength*sizeof(int));
m_lengthDistributionDouble = new double[m_maxlength];
for (int i = 0; i < m_maxlength; i++)
m_lengthDistributionDouble[i] = (double) m_lengthDistribution[i];
m_mass = h_calcMass(m_lengthDistribution, maxlength);
int* temp_lengthDistribution = new int[m_mass];
for(int i = 0; i < m_mass; i++)
{
if(i < maxlength)
temp_lengthDistribution[i] = m_lengthDistribution[i];
else
temp_lengthDistribution[i] = 0;
}
//m_lengthDistribution = temp_lengthDistribution;
for (int i = 0; i < maxlength; i++)
{
m_base[i] = 0;
}
for (int i = 0; i < m_nonZeroCount; i++)
{
m_nonZerosIndicesCrop[i] = 0;
m_baseStateHolder[i] = 0;
m_aggStateHolder[i] = 0;
m_fragStateHolder[i] = 0;
m_futureStateHolder[i] = 0;
m_aggLDHolder[i] = 0;
m_fragLDHolder[i] = 0;
m_futureLDHolder[i] = 0;
}
m_mlThreads = 8;
if ( (maxlength % m_mlThreads) ==0 )
m_mlBlocks = maxlength/m_mlThreads;
else
m_mlBlocks = maxlength/m_mlThreads+1;
m_transitProb = new double[m_futureStateCount];
m_transitRanges = new double[m_futureStateCount];
h_calcFreeEnergy();
}
LengthDistribution::LengthDistribution(int*initialDistribution,int maxlength,int debug)
{
m_maxlength=maxlength;
调试=调试;
m_非零计数=0;
m_nonzerosincrop=新整数[m_nonZeroCount];
m_baseStateHolder=新整数*[m_非零计数];
m_futureStateCount=0;
m_aggstatecont=0;
m_fragStateCount=0;
m_base=新整数[maxlength];
m_myfreenergy=0;
m_r=0;
m_s=0;
m_q=0;
m_stateOrigin=0;
m_分区函数=1;
m_FuturedHolder=新长度分布*[m_非零计数];
m_futureStateHolder=新整数*[m_非零计数];
m_myTransitProb=0;
m_lengthDistribution=新整数[m_maxlength];
memcpy(m_length分布,initialDistribution,m_maxlength*sizeof(int));
m_lengthDistributionDouble=新的双精度[m_maxlength];
对于(int i=0;i
我真的很感激一些想法和建议。自学的C++一直充满了苦恼。教自己停止使用<代码>新< /代码>和双指针直到你接近专家。我怀疑所有这些代码都是重现问题所必需的。尝试删除位,直到问题消失;当你意识到最后一件被移除的东西是什么时,你甚至可以自己找到它。不管怎么说,代码在我看来相当杂乱无章,有点C风格;你可能想读一本书。
LengthDistribution::LengthDistribution(int* initialDistribution, int maxlength, int debug)
{
m_maxlength = maxlength;
m_debug = debug;
m_nonZeroCount = 0;
m_nonZerosIndicesCrop = new int[m_nonZeroCount];
m_baseStateHolder = new int*[m_nonZeroCount];
m_futureStateCount=0;
m_aggStateCount=0;
m_fragStateCount=0;
m_base=new int[maxlength];
m_myFreeEnergy=0;
m_r=0;
m_s=0;
m_q=0;
m_stateOrigin=0;
m_partitionFunction=1;
m_futureLDHolder= new LengthDistribution*[m_nonZeroCount];
m_futureStateHolder=new int*[m_nonZeroCount];
m_myTransitProb = 0;
m_lengthDistribution = new int[m_maxlength];
memcpy(m_lengthDistribution, initialDistribution, m_maxlength*sizeof(int));
m_lengthDistributionDouble = new double[m_maxlength];
for (int i = 0; i < m_maxlength; i++)
m_lengthDistributionDouble[i] = (double) m_lengthDistribution[i];
m_mass = h_calcMass(m_lengthDistribution, maxlength);
int* temp_lengthDistribution = new int[m_mass];
for(int i = 0; i < m_mass; i++)
{
if(i < maxlength)
temp_lengthDistribution[i] = m_lengthDistribution[i];
else
temp_lengthDistribution[i] = 0;
}
//m_lengthDistribution = temp_lengthDistribution;
for (int i = 0; i < maxlength; i++)
{
m_base[i] = 0;
}
for (int i = 0; i < m_nonZeroCount; i++)
{
m_nonZerosIndicesCrop[i] = 0;
m_baseStateHolder[i] = 0;
m_aggStateHolder[i] = 0;
m_fragStateHolder[i] = 0;
m_futureStateHolder[i] = 0;
m_aggLDHolder[i] = 0;
m_fragLDHolder[i] = 0;
m_futureLDHolder[i] = 0;
}
m_mlThreads = 8;
if ( (maxlength % m_mlThreads) ==0 )
m_mlBlocks = maxlength/m_mlThreads;
else
m_mlBlocks = maxlength/m_mlThreads+1;
m_transitProb = new double[m_futureStateCount];
m_transitRanges = new double[m_futureStateCount];
h_calcFreeEnergy();
}