Sto facendo un programma per la tesi, ma sono incappato in un problema da cui non so come uscire:
*** glibc detected *** /home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo: double free or corruption (!prev): 0x0816c238 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb74f7ee2]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb76f451f]
/home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo[0x804dcf3]
/home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo[0x804cfff]
/home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo[0x804c2cf]
/home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo[0x804bd88]
/home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo[0x804e968]
/home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo[0x804e414]
/home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo[0x804d825]
/home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo[0x804cc22]
/home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo[0x804fd55]
/home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo[0x804fcae]
/home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo[0x804b440]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb749b4d3]
/home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo[0x8049e21]
======= Memory map: ========
08048000-08054000 r-xp 00000000 08:06 2668038 /home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo
08054000-08055000 r--p 0000b000 08:06 2668038 /home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo
08055000-08056000 rw-p 0000c000 08:06 2668038 /home/fabio/Desktop/0.019_caccia_al_glich/bin/Debug/altrotentativo
0816b000-0818c000 rw-p 00000000 00:00 0 [heap]
b7454000-b7456000 rw-p 00000000 00:00 0
b7456000-b7480000 r-xp 00000000 08:06 2019972 /lib/i386-linux-gnu/libm-2.15.so
b7480000-b7481000 r--p 00029000 08:06 2019972 /lib/i386-linux-gnu/libm-2.15.so
b7481000-b7482000 rw-p 0002a000 08:06 2019972 /lib/i386-linux-gnu/libm-2.15.so
b7482000-b7626000 r-xp 00000000 08:06 2019977 /lib/i386-linux-gnu/libc-2.15.so
b7626000-b7628000 r--p 001a4000 08:06 2019977 /lib/i386-linux-gnu/libc-2.15.so
b7628000-b7629000 rw-p 001a6000 08:06 2019977 /lib/i386-linux-gnu/libc-2.15.so
b7629000-b762d000 rw-p 00000000 00:00 0
b762d000-b7649000 r-xp 00000000 08:06 1967028 /lib/i386-linux-gnu/libgcc_s.so.1
b7649000-b764a000 r--p 0001b000 08:06 1967028 /lib/i386-linux-gnu/libgcc_s.so.1
b764a000-b764b000 rw-p 0001c000 08:06 1967028 /lib/i386-linux-gnu/libgcc_s.so.1
b764b000-b7723000 r-xp 00000000 08:06 136141 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b7723000-b7724000 ---p 000d8000 08:06 136141 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b7724000-b7728000 r--p 000d8000 08:06 136141 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b7728000-b7729000 rw-p 000dc000 08:06 136141 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b7729000-b7730000 rw-p 00000000 00:00 0
b7748000-b774c000 rw-p 00000000 00:00 0
b774c000-b774d000 r-xp 00000000 00:00 0 [vdso]
b774d000-b776d000 r-xp 00000000 08:06 2019967 /lib/i386-linux-gnu/ld-2.15.so
b776d000-b776e000 r--p 0001f000 08:06 2019967 /lib/i386-linux-gnu/ld-2.15.so
b776e000-b776f000 rw-p 00020000 08:06 2019967 /lib/i386-linux-gnu/ld-2.15.so
bfacb000-bfaec000 rw-p 00000000 00:00 0 [stack]
Aborted (core dumped)
Immagino già che mi direte: "allega il programma"
e quindi eccolo
main.cpp
#include <iostream> // std::cout, std::ios
#include <sstream> // std::ostringstream
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <sstream>
#include <vector>
#include "tool.hpp"
#include <cstdlib>
#include <string>
#include <boost/lexical_cast.hpp>
using namespace std;
int main(){
srand (time(NULL));
int seed;
int N,alfa, T_MAX, Repliche;
double rho;
ifstream NNS("set.txt"); //SU set.txt ci devo mettere l'elenco dei parametri che voglio parsare
//Nell'ordine corretto N alpha rho rho T_max Repliche and so on ripetuti
//tante volte quante volte voglio. IMPORTANTE non fare 2 copie con lo stesso
// valore di N e di rho contemporaneamente!!!
vector<int> vN,valfa,vT_MAX,vRepliche;
vector<double> vrho;
int Numerofile=0;
while (!NNS.eof()){
seed=rand();
NNS>>N;
vN.push_back(N);
NNS>>alfa;
valfa.push_back(alfa);
NNS>>rho;
vrho.push_back(rho);
NNS>>T_MAX;
vT_MAX.push_back(T_MAX);
NNS>>Repliche;
vRepliche.push_back(Repliche);
Numerofile++;
}
NNS.close();
cout<<Numerofile<<endl;
for(int cont=0;cont<Numerofile;cont++){
cout<<"almeno qui ci arrivo"<<endl;
system("sleep 1");
N=vN[cont];
alfa=valfa[cont];
rho=vrho[cont];
T_MAX=vT_MAX[cont];
Repliche=vRepliche[cont];
cout<<"N= "<<N<<"alfa"<<alfa<<"rho"<<rho<<"T"<<T_MAX<<"Rep"<<Repliche<<endl;
int L=N*alfa;
int N2abs=(int)(N+1)*(2*L+1)*rho;
int N_vero=N2abs+1;
int N1,N2;
string NN = boost::lexical_cast<string>(N); //Crea la cartella dati_N=*rho=*
string brho = boost::lexical_cast<string>(rho);
string DIFF = boost::lexical_cast<string>(seed);
std::string s = "mkdir ./dati_N="+NN+"_"+DIFF;
system (s.c_str());
ostringstream hi(ostringstream::ate); //Crea il file hist indati_N=*rho=*
hi<<"./dati_N="<<N<<"_"<<seed<<"/hist.txt";
ofstream hist(hi.str().c_str());
ostringstream hi2(ostringstream::ate); //Crea il file parametri indati_N=*rho=*
hi2<<"./dati_N="<<N<<"_"<<seed<<"/parametri.txt";
ofstream param(hi2.str().c_str());
ostringstream Ndi(ostringstream::ate); //Crea il file N(t)
Ndi<<"./dati_N="<<N<<"_"<<seed<<"/NdiT.txt";
ofstream N_di_t(Ndi.str().c_str());
ostringstream co(ostringstream::ate); //Crea il file correlatori
co<<"./dati_N="<<N<<"_"<<seed<<"/correlatori.txt";
ofstream corr(co.str().c_str());
param<<"N= "<<N<<endl;
param<<"L= "<<L<<endl;
param<<"N1= "<<1<<endl;
param<<"N2= "<<N2abs<<endl;
param<<"Ntot= "<<N_vero<<endl;
param<<"rho= "<<rho<<endl;
param<<"T_max= "<<T_MAX<<endl;
param<<"Repliche= "<<Repliche<<endl;
param.close();
vector<int> G(2*L,0); //Creo le matrici di vector e le inizializzo a 0
LATTICE A1;
LATTICE A2;
LATTICE L1;
LATTICE L2;
for(int i=0;i<N;i++){
A1.LL.push_back(G);
A2.LL.push_back(G);
L1.LL.push_back(G);
L2.LL.push_back(G);
}
double HIST[T_MAX]; //Creo il vettore HIST che andrà poi scritto su N_di_t
for (int i=0;i<T_MAX;i++){HIST[i]=0;}
double CORR[T_MAX][4]; //Creo i 4 vettori CORR che andranno poi scritti su corr
for (int i=0;i<T_MAX;i++){CORR[i][0]=0;CORR[i][1]=0;CORR[i][2]=0;CORR[i][3]=0;}
//Su hist invece scrivo direttamente in modo "dinamico"
//poco alla volta di mano in mano che escono i risultati.
for(int I=0;I<Repliche;I++){
N1=1;
N2=N2abs;
HIST[0]+=N1;
L1.azzera(N,L);
L2.azzera(N,L);
A1.azzera(N,L);
A2.azzera(N,L);
int PP=rand()%N;
if (PP%2==0){L1.LL[PP][2*(rand()%(L))-1]++;}
else {L1.LL[PP][2*(rand()%(L))]++;}
//inizializzo a random le N1 particelle di tipo1
for(int i=0;i<N2;i++){
PP=rand()%N;
if (PP%2==0){L2.LL[PP][2*(rand()%(L))-1]++;}
else {L2.LL[PP][2*(rand()%(L))]++;}
}
bool flag=0;
for (int k=0;k<T_MAX;k++){
evolvi(N,L,L1,L2,A1,A2);
ricombina(A1,A2,N,L);
N1=A1.conta(N,L);
N2=A2.conta(N,L);
for(int i=0;i<N;i++){
for(int j=0;j<2*L+1;j++){
L1.LL[i][j]=A1.LL[i][j];
L2.LL[i][j]=A2.LL[i][j];
}
}
A1.azzera(N,L);
A2.azzera(N,L);
CORR[k][0]+=corr_b(L1,N,L);
CORR[k][1]+=corr_b(L2,N,L);
CORR[k][2]+=corr_d(L1,N,L);
CORR[k][3]+=corr_d(L2,N,L);
HIST[k+1]+=N1;
if(N2==0){if(flag==0){flag=1;hist<<k<<endl;}}
}
}
for(int i=0;i<T_MAX;i++){
N_di_t<<HIST[i]/Repliche<<endl;
corr<<CORR[i][0]/Repliche<<" "<<CORR[i][1]/Repliche<<" "<<CORR[i][2]/Repliche<<" "<<CORR[i][3]/Repliche<<endl;
}
cout<<"anche qui"<<endl;
system("sleep 1");
hist.close();
N_di_t.close();
corr.close();
L1.~LATTICE();
L2.~LATTICE();
A2.~LATTICE();
A1.~LATTICE();
cout<<"yeeeee"<<endl;
}
}
tool.cpp
#include "tool.hpp"
#include <iostream> // std::cout, std::ios
#include <sstream> // std::ostringstream
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <sstream>
#include <vector>
using namespace std;
int mod (int a, int b){
if(b==0)return 0;
if (a>0){return (a%b);}
return (b-1);
}
void ricombina(LATTICE &F1,LATTICE &F2, int &N, int &L){
int app;
for (int I=0;I<N;I++){
for (int J=0;J<2*L+1;J++){
if((F1.LL[I][J]!=0)&&(F2.LL[I][J]!=0)){
if(F1.LL[I][J]>=F2.LL[I][J]){
F1.LL[I][J]+=F2.LL[I][J];
F2.LL[I][J]=0;
}
if(F2.LL[I][J]>F1.LL[I][J]){
app=F2.LL[I][J]-F1.LL[I][J];
F1.LL[I][J]=F1.LL[I][J]*2;
F2.LL[I][J]=app;
}
}
}
}
}
char evolvino(int &x, int &y, int &N, int &L){
if(y==2*L){return 's';}
if(y==0 ){return 'w';}
if(y!=(L-1)){
switch (rand()%2){
case 0:
return 'w';
break;
case 1:
return 's';
break;
}
}
switch (rand()%4){
case 0:
return 'w';
break;
case 1:
return 'a';
break;
case 2:
return 's';
break;
case 3:
return 'd';
break;
}
}
void evolvi(int N,int L,LATTICE &L1,LATTICE &L2,LATTICE &A1,LATTICE &A2){
char r;
for(int i=0;i<N;i++){
for(int j=0;j<2*L+1;j++){ //scorro tutti i siti
while(L1.LL[i][j]!=0){
r=evolvino(i,j,N,L);
if(r=='w'){A1.LL[i][j+1]++;}
if(r=='a'){A1.LL[mod(i-1,N-1)][j]++;}
if(r=='s'){A1.LL[i][j-1]++;}
if(r=='d'){A1.LL[mod(i+1,N-1)][j]++;}
L1.LL[i][j]--;
}
while(L2.LL[i][j]!=0){
r=evolvino(i,j,N,L);
if(r=='w'){A2.LL[i][j+1]++;}
if(r=='a'){A2.LL[mod(i-1,N-1)][j]++;}
if(r=='s'){A2.LL[i][j-1]++;}
if(r=='d'){A2.LL[mod(i+1,N-1)][j]++;}
L2.LL[i][j]--;
}
}
}
}
LATTICE::~LATTICE( )
{
cout << "Fine del reticolo\n";
}
tools.hpp
#include <iostream> // std::cout, std::ios
#include <sstream> // std::ostringstream
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <sstream>
#include <vector>
using namespace std;
class LATTICE{
private:
public:
~LATTICE();
vector<vector<int> > LL;
void stampa(int N,int L){
cout<<"___________"<<endl;
for(int j=0;j<2*L+1;j++){
for(int i=0;i<N;i++){
cout<<LL[i][j]<<" ";
}
cout<<endl;
}
cout<<"___________"<<endl;
}
int conta(int &N,int &L){
int RES=0;
for(int i=0;i<N;i++){
for(int j=0;j<2*L+1;j++){
RES=RES+LL[i][j];
}
}
return RES;
}
void azzera(int &N, int &L){
for (int i=0;i<N;i++){
for (int j=0;j<2*L+1;j++){
LL[i][j]=0;}
}
}
};
int mod (int a, int b);
void ricombina(LATTICE &F1,LATTICE &F2, int &N, int &L);
char evolvino(int &x, int &y, int &N, int &L);
void evolvi(int N,int L,LATTICE &L1,LATTICE &L2,LATTICE &A1,LATTICE &A2);
int corr_b(LATTICE &L1,int &N,int &L);
int corr_d(LATTICE &L1,int &N,int &L);
int conta_N1(LATTICE &L1,int &N,int &L);
void printa(LATTICE &L1,LATTICE &L2,int N,int L);
void carica(int &N, int &alfa, double &rho1, double &beta, int &T_MAX, int &Repliche);
a parte la pessima economia del programma... dove sta l'errore logico grave?