2

ループ反復中に 2D 動的配列の値が破損しています。たとえば、ループの決定/反復 16 ではすべての値が正しく表示されますが、決定/反復 26 になると、反復中に配列の場所 KillRate 2が破損し、値が割り当てられていません。 KillRate 配列。添付の画像に示されているように。

for(int i=0;i<26;i++)
{
    for(int j=0;j<9;j++)
    {
        x[i][j]=0;
        y[i][j]=0;
    }
}
x[0][0]=0;
x[0][1]=.33;
x[0][2]=.75;


x[1][0]=0;
x[1][1]=.33;
x[1][2]=.75;

x[2][0]=0;
x[2][1]=.33;
x[2][2]=.75;

x[3][0]=0;
x[3][1]=.33;
x[3][2]=.75;



    void Display()
{
    int StageValue;
    int DecisionValue;
    double long Minimum;
    TreeNodeType *T;
    cout<<"**********************@@@@@DISPLAY STARTED@@@@**************************************"<<endl;
    for(TreeNodeType *p=Headptr;p!=NULL;p=p->FirstChild)
    {

        KillRate[3][0]=0;
        KillRate[3][1]=.33;
        KillRate[3][2]=.75;


        DecisionValue=-1;
        cout<<"\n\n\n"<<endl;
        for(TreeNodeType *q=p;q!=NULL;q=q->Siblings)
        {
            if(q==Headptr)
            {
                q->PestPopulation=pestpop;
            }
            else
            {

                StageValue=q->stage-1;
                DecisionValue=DecisionValue+1;  
                if(DecisionValue==(noOfDecisions))
                {
                    DecisionValue=0;
                }

                double C1=KillRate[StageValue][DecisionValue];
                double K1=Cost[StageValue][DecisionValue];
                cout<<"Stage Value/Decision Value"<<StageValue<<"/"<<DecisionValue<<endl;
                q->PestPopulation=q->Parent->PestPopulation;
                 long double a=(1 - C1);

                 long double b=(PI)*(q->PestPopulation);
                 long double c=(K1)*(this->area);
                 for(int i=0;i<26;i++)
                 {  

                     for(int j=0;j<9;j++)
                     {
                                 cout<<"KillRate:"<<KillRate[i][j]<<" ";//THIS IS THE PLACE WHERE THROUGH ITERATION ARRAY IS BEING DISPLAYED
                     }
                 cout<<endl;
                 }

                cout<<a<<"*"<<b<<"+"<<c<<endl;
                q->Loss= (a*b)+ c;
                TotalLoss[q->stage-1][q->Decision-1]=q->Loss + q->Parent->Loss;
                q->PestPopulation=(1-C1)*(q->PestPopulation)*(this->growthrate);
                cout<<"Stage= "<<q->stage<<", Decision= "<<q->Decision<<",  PestPop="<<q->PestPopulation<<", Loss ="<<q->Loss<<endl<<endl;  


                if(q->Loss>Minimum)
                {
                    Minimum=q->Loss;
                    T=q->Parent;
                }
            }
        }
    }   
    while(T!=Headptr)
    {

        cout<<"Decision Sequence :"<<T->stage<<" / "<<T->Decision<<endl;
        T=T->Parent;
        DecisionSequence[T->stage]=T->Decision;
        cout<<"Decision Sequence :"<<T->Decision<<" ,\t ";
    }
  cout<<"**********************@@@@@DISPLAY ENDED@@@@**************************************"<<endl;

  }

これがツリー構造です

決定第 15 号

これは決定第 26 号です。

4

2 に答える 2

2
KillRate = new double*[26]; 
for(int i=0;i<26;i++) { KillRate[i] = new double[9];}

この割り当ては、メモリを割り当てるだけです。したがって、割り当てられていないアドレスは任意の値になります。C++ ソリューションは次のようになります。

for(int i=0;i<26;i++){
   for(int j=0;j<9;j++){
      KillRate[i][j] = 0;
   } 
} 

またはより高速な c ソリューションcallocまたは_memset

于 2013-07-30T00:56:51.860 に答える
1

これはメモリ管理が原因である可能性があります。同じ問題が発生し、すべてのオブジェクトをポインタにして、新しい ie object* KillRate=new object; を介してメモリを割り当てることでこれを解決しました。

これが役立つことを願っています

于 2013-07-30T00:37:35.837 に答える