1

我是意大利人,很抱歉我的英语不好,如果我在发布这个问题时犯了一些错误,很抱歉,但这是我的第一个问题,所以请对我好一点,谢谢!

我正在写这个内联函数,我知道论坛上有很多类似这个标题的问题,但没有一个可以帮助我!

代码是:

inline int dapotare (int* i_node,int* j_node,int* q_infr,int *value,double *D,double *Q,int level,int nStart,int nEdge,int nInfrastructureTypes,int *risultato)
{int in_esame,attivi=0,inattivi=0,scope,stop;
int *predecessore = new int [nStart] ;
int **numInattivi= new int* [nStart];
int *comulatore = new int [nStart];;
int *BS = new int [nStart];
bool daCentrale=false;              //se true significa che esiste una y_{i,j}^q=1 dove i è il nodo centrale

for(int i=0;i<nStart;i++)
    numInattivi[i] = new int [nStart];

for(int i=0;i<nStart;i++)
    for(int j=0;j<nStart;j++)
        numInattivi[i][j]=0;
for(int i=0;i<=level;i++)
    if(value[i]==1)
        {attivi++;
        if(i_node[i]==nStart-1)
            {daCentrale=true;
            cout<<"il nodo centrale ha almeno un arco uscente!"<<endl;
            }
        }
    else
        {numInattivi[i_node[i]][j_node[i]]++;
        if(numInattivi[i_node[i]][j_node[i]]==nInfrastructureTypes && numInattivi[i_node[i]][j_node[i]]==nInfrastructureTypes)
            inattivi++;
        }
if(attivi>=nStart-1||attivi+inattivi==nEdge*2||daCentrale/*||inattivi>=nEdge-nStart+1*/)
    {risultato[0]=1;
    risultato[1]=attivi;
    risultato[2]=inattivi;
    for(int actual=0;actual<nStart;actual++)
        delete []numInattivi[actual];
    delete []predecessore;
    delete []comulatore;
    delete []BS;
    return 1;
    }

//definisco i predecessori dei nodi
for(int i=0;i<nStart;i++)                       
    predecessore[i]=-1;
for(int i=0;i<=level;i++)
    if(value[i]==1)
            {if(predecessore[i_node[i]]!=-1)
                {cout<<i_node[i]<<" ha almenno due archi uscenti!"<<endl;
                risultato[0]=1;
                risultato[1]=attivi;
                risultato[2]=inattivi;
                for(int actual=0;actual<nStart;actual++)
                    delete []numInattivi[actual];
                delete []numInattivi;
                delete []predecessore;
                delete []comulatore;
                delete []BS;
                return 1;
                }
            else
                {predecessore[i_node[i]]=j_node[i];
                }
            }
    else
            predecessore[i]=-1;

//controllo che il grafo sia aciclico
for(int i=0;i<=level;i++)                       //controllo che il grafo sia aciclico
    {in_esame=predecessore[i_node[i]];
        for(int j=0;j<level&&in_esame!=-1;j++)  //se predecessore[in_esame]==-1 significa che non ho incontrato cicli
            if(predecessore[in_esame]==i_node[i])
                {cout<<"STAMPA DEI PREDECESSORI:"<<endl;
                for(int actual=0;actual<level;actual++)
                    cout<<"predecessore["<<actual<<"]= "<<predecessore[actual]<<endl;
                cout<<"in_esame="<<in_esame<<endl;
                cout<<i_node[i]<<" crea un ciclo!"<<endl;
                risultato[0]=1;
                risultato[1]=attivi;
                risultato[2]=inattivi;
                //cin>>stop;
                for(int actual=0;actual<nStart;actual++)
                    delete []numInattivi[actual];
                delete []predecessore;
                delete []comulatore;
                delete []BS;
                return 1;
                }
            else
                in_esame=predecessore[in_esame];
    }
//definisco il comulatore di ciascun nodo e controllo che nessun nodo del grafo parziale sia sovraccaricato
for(int i=0;i<nStart-1;i++)     //inizializziamo comulatore
        comulatore[i]=-D[i];
for(int i=0;i<nStart-1;i++)     //definiamo BS
        {BS[i]=0;
        for(int j=0;j<nStart-1;j++)
            if(predecessore[j]==i)  //j è figlio di i
                BS[i]++;
        }
for(int i=0;i<nStart-1;i++)
    cout<<"D["<<i<<"]= "<<D[i]<<endl;
for(int i=0;i<nStart-1;i++)
    cout<<"predecessore["<<i<<"]= "<<predecessore[i]<<endl;
for(int i=0;i<nStart-1;i++)
    cout<<"BS["<<i<<"]= "<<BS[i]<<endl;
cout<<"Prima del comulatore"<<endl;
for(int i=0;i<nStart-1;i++)
        {for(int j=0;j<nStart-1;j++)
            {for(int actual=0;actual<=level;actual++)
                {if(i_node[actual]==j&&value[actual]==1&&BS[j]==0)
                    {comulatore[predecessore[j]]+=comulatore[j];
                    BS[predecessore[j]]--;
                    BS[j]=-1;
                    }
                }
            }
        }
for(int i=0;i<nStart-1;i++)
    cout<<"comulatore["<<i<<"]= "<<comulatore[i]<<endl;
for(int actual=0;actual<=level;actual++)
    {if(comulatore[i_node[actual]]>Q[q_infr[actual]]&&value[actual]==1)
        {cout<<i_node[actual]<<" è sovraccaricato: comulaotre["<<i_node[actual]<<"]= "<<comulatore[i_node[actual]]<<" Q[q_infr]="<<Q[q_infr[actual]]<<endl;
        risultato[0]=1;
        risultato[1]=attivi;
        risultato[2]=inattivi;
        for(int actual=0;actual<nStart;actual++)
            delete []numInattivi[actual];
        delete []predecessore;
        delete []comulatore;
        delete []BS;
        return 1;
        }
    }

risultato[0]=0;             //Tutto ok! Non ci interessa il valore degli inattivi e inattivi se non c'è niente da potare
risultato[1]=0;
risultato[2]=0;
for(int actual=0;actual<nStart;actual++)
    delete[] numInattivi[actual];
cout<<"superato il for"<<endl;
delete[] numInattivi;
cout<<"superato numInattivi"<<endl;
delete[] predecessore;
cout<<"superato il predecessore"<<endl;
delete[] comulatore;
cout<<"superato comulatore"<<endl;
delete[] BS;
cout<<"superato BS"<<endl;
return 0;
}

我运行它时返回的错误是:

  *** glibc detected *** ./main: free(): invalid next size (fast): 0x09cb7d48 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb6567ee2]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb678f51f]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdaPv+0x1b)[0xb678f57b]
./main[0x8057312]
./main[0x805591a]
./main[0x804a9ab]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb650b4d3]
./main[0x804abd5]
======= Memory map: ========
08048000-0805a000 r-xp 00000000 07:00 2360       /home/luca-kun/Desktop/LOG/BranchAndBound/main
0805a000-0805b000 r--p 00011000 07:00 2360       /home/luca-kun/Desktop/LOG/BranchAndBound/main
0805b000-0805c000 rw-p 00012000 07:00 2360       /home/luca-kun/Desktop/LOG/BranchAndBound/main
09cb3000-09cd4000 rw-p 00000000 00:00 0          [heap]
b5843000-b5846000 rw-p 00000000 00:00 0 
b5846000-b5848000 r-xp 00000000 07:00 590098     /lib/i386-linux-gnu/libutil-2.15.so
b5848000-b5849000 r--p 00001000 07:00 590098     /lib/i386-linux-gnu/libutil-2.15.so
b5849000-b584a000 rw-p 00002000 07:00 590098     /lib/i386-linux-gnu/libutil-2.15.so
b584a000-b584b000 rw-p 00000000 00:00 0 
b584b000-b584e000 r-xp 00000000 07:00 590114     /lib/i386-linux-gnu/libdl-2.15.so
b584e000-b584f000 r--p 00002000 07:00 590114     /lib/i386-linux-gnu/libdl-2.15.so
b584f000-b5850000 rw-p 00003000 07:00 590114     /lib/i386-linux-gnu/libdl-2.15.so
b5850000-b589f000 r-xp 00000000 07:00 297406     /usr/lib/openmpi/lib/libopen-pal.so.0.0.0
b589f000-b58a0000 r--p 0004e000 07:00 297406     /usr/lib/openmpi/lib/libopen-pal.so.0.0.0
b58a0000-b58a2000 rw-p 0004f000 07:00 297406     /usr/lib/openmpi/lib/libopen-pal.so.0.0.0
b58a2000-b58a4000 rw-p 00000000 00:00 0 
b58a4000-b58ed000 r-xp 00000000 07:00 297407     /usr/lib/openmpi/lib/libopen-rte.so.0.0.0
b58ed000-b58ee000 r--p 00048000 07:00 297407     /usr/lib/openmpi/lib/libopen-rte.so.0.0.0
b58ee000-b58f0000 rw-p 00049000 07:00 297407     /usr/lib/openmpi/lib/libopen-rte.so.0.0.0
b58f0000-b58f1000 rw-p 00000000 00:00 0 
b58f1000-b58f2000 r-xp 00000000 07:00 300602     /usr/lib/libblacsCinit-openmpi.so.1.1
b58f2000-b58f3000 r--p 00000000 07:00 300602     /usr/lib/libblacsCinit-openmpi.so.1.1
b58f3000-b58f4000 rw-p 00001000 07:00 300602     /usr/lib/libblacsCinit-openmpi.so.1.1
b58f4000-b5987000 r-xp 00000000 07:00 297402     /usr/lib/openmpi/lib/libmpi.so.0.0.2
b5987000-b5988000 r--p 00093000 07:00 297402     /usr/lib/openmpi/lib/libmpi.so.0.0.2
b5988000-b5995000 rw-p 00094000 07:00 297402     /usr/lib/openmpi/lib/libmpi.so.0.0.2
b5995000-b599e000 rw-p 00000000 00:00 0 
b599e000-b59b3000 r-xp 00000000 07:00 300611     /usr/lib/libpord-4.9.2.so
b59b3000-b59b4000 r--p 00014000 07:00 300611     /usr/lib/libpord-4.9.2.so
b59b4000-b59b5000 rw-p 00015000 07:00 300611     /usr/lib/libpord-4.9.2.so
b59b5000-b5a27000 r-xp 00000000 07:00 521780     /usr/lib/i386-linux-gnu/libquadmath.so.0.0.0
b5a27000-b5a28000 r--p 00071000 07:00 521780     /usr/lib/i386-linux-gnu/libquadmath.so.0.0.0
b5a28000-b5a29000 rw-p 00072000 07:00 521780     /usr/lib/i386-linux-gnu/libquadmath.so.0.0.0
b5a29000-b5a5a000 r-xp 00000000 07:00 300598     /usr/lib/libblacs-openmpi.so.1.1
b5a5a000-b5a5b000 r--p 00030000 07:00 300598     /usr/lib/libblacs-openmpi.so.1.1
b5a5b000-b5a5c000 rw-p 00031000 07:00 300598     /usr/lib/libblacs-openmpi.so.1.1
b5a5c000-b6040000 r-xp 00000000 07:00 300606     /usr/lib/libscalapack-openmpi.so.1.8.0
b6040000-b6041000 r--p 005e3000 07:00 300606     /usr/lib/libscalapack-openmpi.so.1.8.0
b6041000-b6043000 rw-p 005e4000 07:00 300606     /usr/lib/libscalapack-openmpi.so.1.8.0
b6043000-b6095000 rw-p 00000000 00:00 0 
b6095000-b60c0000 r-xp 00000000 07:00 297404     /usr/lib/openmpi/lib/libmpi_f77.so.0.0.1
b60c0000-b60c1000 r--p 0002a000 07:00 297404     /usr/lib/openmpi/lib/libmpi_f77.so.0.0.1
b60c1000-b60c2000 rw-p 0002b000 07:00 297404     /usr/lib/openmpi/lib/libmpi_f77.so.0.0.1
b60c2000-b60c3000 rw-p 00000000 00:00 0 
b60c3000-b60ff000 r-xp 00000000 07:00 300610     /usr/lib/libmumps_common-4.9.2.so
b60ff000-b6100000 r--p 0003b000 07:00 300610     /usr/lib/libmumps_common-4.9.2.so
b6100000-b6101000 rw-p 0003c000 07:00 300610     /usr/lib/libmumps_common-4.9.2.so
b6101000-b61ff000 r-xp 00000000 07:00 568549     /usr/lib/i386-linux-gnu/libgfortran.so.3.0.0
b61ff000-b6200000 r--p 000fe000 07:00 568549     /usr/lib/i386-linux-gnu/libgfortran.so.3.0.0
b6200000-b6201000 rw-p 000ff000 07:00 568549     /usr/lib/i386-linux-gnu/libgfortran.so.3.0.0
b6201000-b6202000 rw-p 00000000 00:00 0 
b6202000-b64d3000 r-xp 00000000 07:00 298969     /usr/lib/atlas-base/atlas/libblas.so.3gf.0
b64d3000-b64d7000 rw-p 002d1000 07:00 298969     /usr/lib/atlas-base/atlas/libblas.so.3gf.0
b64d7000-b64ee000 r-xp 00000000 07:00 590108     /lib/i386-linux-gnu/libpthread-2.15.so
b64ee000-b64ef000 r--p 00016000 07:00 590108     /lib/i386-linux-gnu/libpthread-2.15.so
b64ef000-b64f0000 rw-p 00017000 07:00 590108     /lib/i386-linux-gnu/libpthread-2.15.so
b64f0000-b64f2000 rw-p 00000000 00:00 0 
b64f2000-b6695000 r-xp 00000000 07:00 590099     /lib/i386-linux-gnu/libc-2.15.so
b6695000-b6697000 r--p 001a3000 07:00 590099     /lib/i386-linux-gnu/libc-2.15.so
b6697000-b6698000 rw-p 001a5000 07:00 590099     /lib/i386-linux-gnu/libc-2.15.so
b6698000-b669c000 rw-p 00000000 00:00 0 
b669c000-b66b8000 r-xp 00000000 07:00 554764     /lib/i386-linux-gnu/libgcc_s.so.1
b66b8000-b66b9000 r--p 0001b000 07:00 554764     /lib/i386-linux-gnu/libgcc_s.so.1
b66b9000-b66ba000 rw-p 0001c000 07:00 554764     /lib/i386-linux-gnu/libgcc_s.so.1
b66ba000-b66e4000 r-xp 00000000 07:00 590110     /lib/i386-linux-gnu/libm-2.15.so
b66e4000-b66e5000 r--p 00029000 07:00 590110     /lib/i386-linux-gnu/libm-2.15.so
b66e5000-b66e6000 rw-p 0002a000 07:00 590110     /lib/i386-linux-gnu/libm-2.15.so
b66e6000-b67be000 r-xp 00000000 07:00 527383     /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b67be000-b67bf000 ---p 000d8000 07:00 527383     /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b67bf000-b67c3000 r--p 000d8000 07:00 527383     /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b67c3000-b67c4000 rw-p 000dc000 07:00 527383     /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b67c4000-b67cb000 rw-p 00000000 00:00 0 
b67cb000-b68f2000 r-xp 00000000 07:00 300609     /usr/lib/libdmumps-4.9.2.so
b68f2000-b68f3000 r--p 00126000 07:00 300609     /usr/lib/libdmumps-4.9.2.so
b68f3000-b68f4000 rw-p 00127000 07:00 300609     /usr/lib/libdmumps-4.9.2.so
b68f4000-b68f5000 rw-p 00000000 00:00 0 
b68f5000-b6909000 r-xp 00000000 07:00 521548     /lib/i386-linux-gnu/libz.so.1.2.3.4
b6909000-b690a000 r--p 00013000 07:00 521548     /lib/i386-linux-gnu/libz.so.1.2.3.4
b690a000-b690b000 rw-p 00014000 07:00 521548     /lib/i386-linux-gnu/libz.so.1.2.3.4
b690b000-b690c000 rw-p 00000000 00:00 0 
b690c000-b70d3000 r-xp 00000000 07:00 298970     /usr/lib/atlas-base/atlas/liblapack.so.3gf.0
b70d3000-b70d7000 rw-p 007c6000 07:00 298970     /usr/lib/atlas-base/atlas/liblapack.so.3gf.0
b70d7000-b71e4000 rw-p 00000000 00:00 0 
b71e4000-b72fd000 r-xp 00000000 07:00 300909     /lib/libCoinUtils.so.3.8.5
b72fd000-b7300000 rw-p 00118000 07:00 300909     /lib/libCoinUtils.so.3.8.5
b7300000-b734d000 r-xp 00000000 07:00 300939     /lib/libOsi.so.1.10.2
b734d000-b734f000 rw-p 0004d000 07:00 300939     /lib/libOsi.so.1.10.2
b734f000-b74b8000 r-xp 00000000 07:00 300887     /lib/libClp.so.1.11.5
b74b8000-b74bb000 rw-p 00169000 07:00 300887     /lib/libClp.so.1.11.5
b74bb000-b759d000 r-xp 00000000 07:00 300885     /lib/libCgl.so.1.7.2
b759d000-b759f000 rw-p 000e1000 07:00 300885     /lib/libCgl.so.1.7.2
b759f000-b768b000 r-xp 00000000 07:00 300877     /lib/libCbc.so.3.7.5
b768b000-b7690000 rw-p 000ec000 07:00 300877     /lib/libCbc.so.3.7.5
b7690000-b7691000 rw-p 00000000 00:00 0 
b7691000-b76cd000 r-xp 00000000 07:00 300943     /lib/libOsiClp.so.1.11.5
b76cd000-b76ce000 rw-p 0003c000 07:00 300943     /lib/libOsiClp.so.1.11.5
b76ce000-b76da000 r-xp 00000000 07:00 262019     /lib/libOsiCbc.so.3.7.5
b76da000-b76db000 rw-p 0000b000 07:00 262019     /lib/libOsiCbc.so.3.7.5
b76f1000-b76f7000 rw-p 00000000 00:00 0 
b76f7000-b76f8000 r-xp 00000000 00:00 0          [vdso]
b76f8000-b7718000 r-xp 00000000 07:00 590111     /lib/i386-linux-gnu/ld-2.15.so
b7718000-b7719000 r--p 0001f000 07:00 590111     /lib/i386-linux-gnu/ld-2.15.so
b7719000-b771a000 rw-p 00020000 07:00 590111     /lib/i386-linux-gnu/ld-2.15.so
bf951000-bf972000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)

谁能帮我?感谢所有回答我问题的人!

4

2 回答 2

3

你不能混合 Cmalloc和 C++ delete/delete[]

  • 分配的内存malloc必须用 释放free

  • 分配的内存new必须用 释放delete

  • 分配的内存new[]必须用 释放delete[]

其他任何事情都是未定义的行为。

现在,在您编辑程序并确保所有分配都由完成new[]并删除delete[]之后,错误的原因是某处的缓冲区溢出。

于 2013-08-22T19:37:49.530 回答
0

这段代码真是一团糟。也就是说,我看到了很多奇怪的事情:

1) 谁在分配risultato?是否为它分配了三个整数?

2)代码这样做

if(numInattivi[i_node[i]][j_node[i]]==nInfrastructureTypes &&
   numInattivi[i_node[i]][j_node[i]]==nInfrastructureTypes)

你真的认为检查两次同样的事情会有帮助吗?

3) 循环for(int i=0;i<=level;i++)使用level+1元素、arevaluei_node足够j_node大吗?

4)您正在泄漏numInattivi(并非总是如此)。为什么所有这些显式分配和指针摆弄而不是仅仅使用std::vector?相同的速度,但容易使用。

5)predecessore看起来像按节点编号索引,但有时它直接在同一个循环中使用i=0,1,...,level而不是i_node[i]or进行索引j_node[i]。它是否正确?

6)如果程序的其余部分以相同的方式编写,那么您可能到处都在弄乱内存,并且没有希望让这个东西正常运行。您在此函数中遇到错误这一事实没有任何意义......用内存做坏事可能会在以后触发数百万条执行指令的崩溃。

于 2013-08-22T20:16:07.033 回答