2012-02-13 9 views
2

私はプログラミングについての質問に新しいです。そうすれば、正しい方法で正しいことをやっていないなら...許してください。*** glibcが検出されました*** ...:free():無効な次のサイズ(通常):... *** fclose();

私は "単純な" ANSI Cプログラミングに慣れていましたが、私の "ポケット"に客観的なプログラミング技術を追加しようと決めました。 最初は、 "new"を使うことはできませんでした。実行時に2次元配列を割り当てることはできませんでしたが、結局私はあきらめてコールコを使いました。

しかし、今私はfcloseを使用しているときにこのエラーが発生します - そして、どのように引き起こされたのか分かりません...助けてください。

私は同じようなスレッドを最初に読んだが、今回はそれは私を助けなかった。

これは小さくて非常に単純なプログラムなので、コード全体をコピーして、問題が何であるかを誰かが知ってくれることを願っています。

#include <iostream> 
#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 
#include <time.h> 
#include <fstream> 
#include <vector> 
#include <new> 
#include <malloc.h> 

#define min(a,b) ((a) < (b)) ? (a) : (b) 
#define square(a) a*a 
#define cube(a, b, c) a*b*c 

using namespace std; 

class AlphaTime 
{ 
    long *alpha; 
    long *time; 
    long **NeighbourCells; 

    private: 
    bool set_NeighbourCells (long); 
    bool initualize_alpha_t(long); 

    public: 
    bool get_NeighbourCells(long); 
    bool init_NeighbourCells (long); 
    bool init_alpha_t(long); 
    void set_initial_places (long, long); 
    bool run(long, long); 
    bool count(long, long); 
    bool print(long, long, long); 
}; 

/**********************************************************************************/ 

bool AlphaTime::init_alpha_t(long Lmax) 
{ 
bool err; 
return (err=initualize_alpha_t(Lmax)); 
} 

/**********************************************************************************/ 

bool AlphaTime::initualize_alpha_t(long Lmax) 
{ 
bool err=0; 
long i; 
alpha = (long*) calloc((size_t) cube(Lmax,Lmax,Lmax), sizeof(long)); 
if(alpha==NULL) {err=1; goto RET;} 
for(i = 0; i < cube(Lmax, Lmax, Lmax); i++) 
    alpha[i]=0; 
RET:  
return(err); 
} 

/**********************************************************************************/ 

int main(int argc, char *farg[]) 
{ 
int i; 
bool errflag=0, End=0; 
long Conc, Lmax, step; 
long Lmax3; 

AlphaTime avrami; 
if (argc != 3) 
    goto ERR; 
Lmax = atol (farg[2]); 
Conc = atol (farg[1]); 
Lmax3 = cube(Lmax, Lmax, Lmax); 
printf("Conc = %ld, Lmax = %ld\n",Conc, Lmax); 

if(errflag=avrami.init_alpha_t(Lmax)) goto ERR_AR; 
if(errflag=avrami.init_NeighbourCells(Lmax)) goto ERR_AR; 
avrami.set_initial_places(Conc, Lmax); 

for(step = 2; step < Lmax3; step++) 
{ 
    End = avrami.run(Lmax, step); 
    if (!End) break; 
} 

if(errflag=avrami.count(Lmax, step)) goto ERR_AR; 
cout<<"step = "<<step<<endl; //getchar(); 
if(errflag=avrami.print(Conc, Lmax, step)) goto ERR_AR; 

exit(0); 

ERR: 
    cout<<"Inaccurate start parameters."; 
    cout<<"\nUsage:\n avrm number_of_skipped_places_for_active_place length_of_cube_edge\n"; 
    exit(-1); 
ERR_AR: 
    cout<<"ERROR while allocating arrays\n"; 
    exit(-1); 
} 

/**********************************************************************************/ 

bool AlphaTime::init_NeighbourCells(long Lmax) 
{ 
bool err; 
return(err=set_NeighbourCells(Lmax)); 
} 

/**********************************************************************************/ 

bool AlphaTime::set_NeighbourCells(long Lmax) 
{ 
bool err=0; 
int i, j, k, t, cell = 0; 
long *a1, row; 
const long Lmax2 = square(Lmax); 
const long Lmax3 = cube(Lmax, Lmax, Lmax); 
const long L2Lm1 = cube(Lmax, Lmax, (Lmax-1)); 

a1 = (long*) calloc((size_t) cube(Lmax,Lmax,Lmax)*7, sizeof(long)); 
NeighbourCells = (long**) calloc((size_t) 7, sizeof(long*)); 
    if(!a1||!NeighbourCells) 
    { 
    err=1; 
    goto RET; 
    } 

for (row=0; row<(cube(Lmax,Lmax,Lmax)); row++) 
{ 
     NeighbourCells[row]=a1+row*7; 
     if(!NeighbourCells[row]) 
     { 
     err=1; 
     goto RET; 
     } 
} 

CHECK: 
    if(!NeighbourCells) 
    { 
    err=1; 
    goto RET; 
    } 

NeighbourCells[0][0]=1; 
NeighbourCells[0][1]=Lmax; 
NeighbourCells[0][2]=(Lmax*Lmax); 
NeighbourCells[0][3]=-1; 
NeighbourCells[0][4]=-1; 
NeighbourCells[0][5]=-1; 
NeighbourCells[0][6]=-1; 


for(k = 1; k < Lmax-1; k++) 
{ 
    NeighbourCells[k][0]=k-1; 
    NeighbourCells[k][1]=k+1; 
    NeighbourCells[k][2]=k+Lmax; 
    NeighbourCells[k][3]=k+Lmax*Lmax; 
    NeighbourCells[k][4]=-1; 
    NeighbourCells[k][5]=-1; 
    NeighbourCells[k][6]=-1; 
} 


NeighbourCells[Lmax-1][0]=Lmax-2; 
NeighbourCells[Lmax-1][1]=2*Lmax-1; 
NeighbourCells[Lmax-1][2]=(Lmax*Lmax+Lmax-1); 
NeighbourCells[Lmax-1][3]=-1; 
NeighbourCells[Lmax-1][4]=-1; 
NeighbourCells[Lmax-1][5]=-1; 
NeighbourCells[Lmax-1][6]=-1; 


for(j = Lmax; j < Lmax*(Lmax-1); j+=Lmax) 
{ 
    //sledwa lqw ryb 
    NeighbourCells[j][0]=j-Lmax; 
    NeighbourCells[j][1]=j+1; 
    NeighbourCells[j][2]=j+Lmax; 
    NeighbourCells[j][3]=Lmax*Lmax+j; 
    NeighbourCells[j][4]=NeighbourCells[j][5]=NeighbourCells[j][6]=-1; 

    for(k = j+1; k < j+Lmax-1; k++) 
    { 
     NeighbourCells[k][0]=k-Lmax; 
     NeighbourCells[k][1]=k-1; 
     NeighbourCells[k][2]=k+1; 
     NeighbourCells[k][3]=k+Lmax; 
     NeighbourCells[k][4]=k+Lmax*Lmax; 
     NeighbourCells[k][5]=NeighbourCells[k][6]=-1; 
    } 


    NeighbourCells[k][0]=k-Lmax; 
    NeighbourCells[k][1]=k-1; 
    NeighbourCells[k][2]=k+Lmax; 
    NeighbourCells[k][3]=Lmax*Lmax+k; 
    NeighbourCells[k][4]=NeighbourCells[k][5]=NeighbourCells[k][6]=-1; 
} 


NeighbourCells[Lmax*(Lmax-1)][0]=Lmax*(Lmax-2); 
NeighbourCells[Lmax*(Lmax-1)][1]=Lmax*(Lmax-1)+1; 
NeighbourCells[Lmax*(Lmax-1)][2]=Lmax*(Lmax-1)+Lmax*Lmax; 
NeighbourCells[Lmax*(Lmax-1)][3]=NeighbourCells[Lmax*(Lmax-1)][4]=NeighbourCells[Lmax*(Lmax-1)][5]=NeighbourCells[Lmax*(Lmax-1)][6]=-1; 


for(k = Lmax*(Lmax-1)+1; k < (Lmax*Lmax-1)-1; k++) 
{ 
    NeighbourCells[k][0]=k-Lmax; 
    NeighbourCells[k][1]=k-1; 
    NeighbourCells[k][2]=k+1; 
    NeighbourCells[k][3]=k+Lmax*Lmax; 
    NeighbourCells[k][4]=NeighbourCells[k][5]=NeighbourCells[k][6]=-1; 
} 


NeighbourCells[Lmax*Lmax-1][0]=Lmax*(Lmax-1)-1; 
NeighbourCells[Lmax*Lmax-1][1]=Lmax*Lmax-2; 
NeighbourCells[Lmax*Lmax-1][2]=Lmax*Lmax-1+Lmax*Lmax; 
NeighbourCells[Lmax*Lmax-1][3]=NeighbourCells[Lmax*Lmax-1][4]=NeighbourCells[Lmax*Lmax-1][5]=NeighbourCells[Lmax*Lmax-1][6]=-1; 

for(i = Lmax2; i < L2Lm1; i+=Lmax2) 
{ 
    NeighbourCells[i][0]=i-Lmax2; 
    NeighbourCells[i][1]=i+1; 
    NeighbourCells[i][2]=i+Lmax; 
    NeighbourCells[i][3]=i+Lmax2; 
    NeighbourCells[i][4]=NeighbourCells[i][5]=NeighbourCells[i][6]=-1; 

    for(k = i+1; k < i+Lmax-1; k++) 
    { 
     NeighbourCells[k][0]=k-Lmax2; 
     NeighbourCells[k][1]=k-1; 
     NeighbourCells[k][2]=k+1; 
     NeighbourCells[k][3]=k+Lmax; 
     NeighbourCells[k][4]=k+Lmax2; 
     NeighbourCells[k][5]=NeighbourCells[k][6]=-1; 
    } 
    NeighbourCells[j+Lmax-1][0]=j+Lmax-1-Lmax2; 
    NeighbourCells[j+Lmax-1][1]=j+Lmax-1-1; 
    NeighbourCells[j+Lmax-1][2]=j+Lmax-1+Lmax; 
    NeighbourCells[j+Lmax-1][3]=j+Lmax-1+Lmax2; 
    NeighbourCells[j+Lmax-1][4]=NeighbourCells[j+Lmax-1][5]=NeighbourCells[j+Lmax-1][6]=-1; 

    for(j = i+Lmax; j < i+Lmax2-Lmax; j+=Lmax) 
    { 
     NeighbourCells[j][0]=j-Lmax2; 
     NeighbourCells[j][1]=j+1; 
     NeighbourCells[j][2]=j+Lmax; 
     NeighbourCells[j][3]=j+Lmax2; 
     NeighbourCells[j][4]=NeighbourCells[j][5]=NeighbourCells[j][6]=-1; 

     for(k = j+1; k < j+Lmax-1; k++) 
     { 
      NeighbourCells[k][0]=k-Lmax2; 
      NeighbourCells[k][1]=k-Lmax; 
      NeighbourCells[k][2]=k-1; 
      NeighbourCells[k][3]=k+1; 
      NeighbourCells[k][4]=k+Lmax; 
      NeighbourCells[k][5]=k+Lmax2; 
      NeighbourCells[k][6]=-1; 
     } //for k 
     NeighbourCells[k][0]=k-Lmax2; 
     NeighbourCells[k][1]=k-Lmax; 
     NeighbourCells[k][2]=k-1; 
     NeighbourCells[k][3]=k+Lmax; 
     NeighbourCells[k][4]=k+Lmax2; 
     NeighbourCells[k][5]=NeighbourCells[k][6]=-1; 
    } //for j 

    NeighbourCells[j][0]=j-Lmax2; 
    NeighbourCells[j][1]=j-Lmax; 
    NeighbourCells[j][2]=j+1; 
    NeighbourCells[j][3]=j+Lmax2; 
    NeighbourCells[j][4]= 
    NeighbourCells[j][4]=NeighbourCells[j][5]=NeighbourCells[j][6]=-1; 
    for(k = j+1; k < j+Lmax-1; k++) 
    { 
     NeighbourCells[k][0]=k-Lmax2; 
     NeighbourCells[k][1]=k-1; 
     NeighbourCells[k][2]=k+1; 
     NeighbourCells[k][3]=k+Lmax2; 
     NeighbourCells[k][4]=NeighbourCells[k][5]=NeighbourCells[k][6]=-1; 
    } 
    NeighbourCells[k][0]=k-Lmax2; 
    NeighbourCells[k][1]=k-Lmax; 
    NeighbourCells[k][2]=k-1; 
    NeighbourCells[k][3]=k+Lmax2; 
    NeighbourCells[k][4]=NeighbourCells[k][5]=NeighbourCells[k][6]=-1; 
}//for i 



NeighbourCells[L2Lm1][0]=L2Lm1-Lmax2; 
NeighbourCells[L2Lm1][1]=L2Lm1+1; 
NeighbourCells[L2Lm1][2]=L2Lm1+Lmax; 
NeighbourCells[L2Lm1][3]=-1; 
NeighbourCells[L2Lm1][4]=-1; 
NeighbourCells[L2Lm1][5]=-1; 
NeighbourCells[L2Lm1][6]=-1; 


for(k = L2Lm1+1; k < L2Lm1+Lmax-1; k++) 
{ 
    NeighbourCells[k][0]=k-Lmax*Lmax; 
    NeighbourCells[k][1]=k-1; 
    NeighbourCells[k][2]=k+1; 
    NeighbourCells[k][3]=k+Lmax; 
    NeighbourCells[k][4]=-1; 
    NeighbourCells[k][5]=-1; 
    NeighbourCells[k][6]=-1; 
} 

NeighbourCells[Lmax3-Lmax2+Lmax-1][0]=Lmax3-Lmax2+Lmax-1-Lmax2; 
NeighbourCells[Lmax3-Lmax2+Lmax-1][1]=Lmax3-Lmax2+Lmax-1-1; 
NeighbourCells[Lmax3-Lmax2+Lmax-1][2]=Lmax3-Lmax2+Lmax-1+Lmax; 
NeighbourCells[Lmax3-Lmax2+Lmax-1][3]=-1; 
NeighbourCells[Lmax3-Lmax2+Lmax-1][4]=-1; 
NeighbourCells[Lmax3-Lmax2+Lmax-1][5]=-1; 
NeighbourCells[Lmax3-Lmax2+Lmax-1][6]=-1; 


for(j = Lmax3-Lmax*(Lmax-1); j < Lmax3-Lmax; j+=Lmax) 
{ 

    NeighbourCells[j][0]=j-Lmax2; 
    NeighbourCells[j][1]=j-Lmax; 
    NeighbourCells[j][2]=j+1; 
    NeighbourCells[j][3]=j+Lmax; 
    NeighbourCells[j][4]=NeighbourCells[j][5]=NeighbourCells[j][6]=-1; 


    for(k = j+1; k < j+Lmax-1; k++) 
    { 
     NeighbourCells[k][0]=k-Lmax2; 
     NeighbourCells[k][1]=k-Lmax; 
     NeighbourCells[k][2]=k-1; 
     NeighbourCells[k][3]=k+1; 
     NeighbourCells[k][4]=k+Lmax; 
     NeighbourCells[k][5]=NeighbourCells[k][6]=-1; 
    } 

    NeighbourCells[k][0]=k-Lmax2; 
    NeighbourCells[k][1]=k-Lmax; 
    NeighbourCells[k][2]=k-1; 
    NeighbourCells[k][3]=k+Lmax; 
    NeighbourCells[k][4]=NeighbourCells[k][5]=NeighbourCells[k][6]=-1; 
} 


NeighbourCells[Lmax3-Lmax][0]=Lmax3-Lmax-Lmax2; 
NeighbourCells[Lmax3-Lmax][1]=Lmax3-Lmax-Lmax; 
NeighbourCells[Lmax3-Lmax][2]=Lmax3-Lmax+1; 
NeighbourCells[Lmax3-Lmax][3]=NeighbourCells[Lmax3-Lmax][4]=NeighbourCells[Lmax3-Lmax][5]=NeighbourCells[Lmax3-Lmax][6]=-1; 


for(k = Lmax3-Lmax+1; k < Lmax3-1; k++) 
{ 

    NeighbourCells[k][0]=k-Lmax2; 
    NeighbourCells[k][1]=k-Lmax; 
    NeighbourCells[k][2]=k-1; 
    NeighbourCells[k][3]=k+1; 
    NeighbourCells[k][4]=NeighbourCells[k][5]=NeighbourCells[k][6]=-1; 
} 


NeighbourCells[Lmax3-1][0]=Lmax3-1-Lmax2; 
NeighbourCells[Lmax3-1][1]=Lmax3-1-Lmax; 
NeighbourCells[Lmax3-1][2]=Lmax3-1-1; 
NeighbourCells[Lmax3-1][3]=-1; 
NeighbourCells[Lmax3-1][4]=-1; 
NeighbourCells[Lmax3-1][5]=-1; 
NeighbourCells[Lmax3-1][6]=-1; 

RET: 
return(err); 
} 

/**********************************************************************************/ 
void AlphaTime::set_initial_places (long Conc, long Lmax) 
{ 
long i, j, k, t, tt, rest, start; 
const long Lmax2 = square(Lmax); 
const long Lmax3 = cube(Lmax, Lmax, Lmax); 
const long L2Lm1 = cube(Lmax, Lmax, (Lmax-1)); 


for(i = (Conc-1); i < Lmax2; i+=Conc) 
{ 
    alpha[i]=1; 
} 


for(i = (Lmax*Conc -1); i < Lmax3; i+=(Lmax*Conc)) 
{ 
    alpha[i]=1; 
} 


j = 0; 
while(!((alpha[j]) && !((j+1)%Lmax))) j++; 
for(i = j; i < Lmax3 - Lmax; i+=Lmax*Conc) 
{ 
    alpha[i]=1; 
} 

for(i = L2Lm1+(Conc-1); i < Lmax3; i+=Conc) 
{ 
    alpha[i]=1; 
} 


for(start = 0, rest = Conc -1, j = start+rest; start < Lmax; start++, j= start*Lmax2+rest) 
{ 
    for(i=j; i<(start*Lmax2+Lmax); i+=Conc) 
    { 
     alpha[i]=1; 
     rest = Lmax2*start+Lmax-1-i; 
    } 
    rest = Conc-1 - rest; 
} 


j = i; 
for(start = 0, rest = Conc -1; start < Lmax; start++, j= start*Lmax2+Lmax2-Lmax+rest) 
{ 
    for(i=j; i<(start*Lmax2+Lmax2); i+=Conc) 
    { 
     alpha[i]=1; 
     rest = start*Lmax2+Lmax2-1-i; 
    } 
    rest = Conc-1 - rest; 
} 
} 

/**********************************************************************************/ 

bool AlphaTime::run(long Lmax, long step) 
{ 
bool More=0; 
const long Lmax3 = cube(Lmax, Lmax, Lmax); 
long i,j; 

for(i = 0; i < Lmax3; i++) 
{ 
    if((!alpha[i])||(alpha[i]==step)) 
    { 
     continue; 
    } 
    j = 0; 
    while(NeighbourCells[i][j]!=-1) 
    { 
      if(!alpha[NeighbourCells[i][j]]) 
      { 
       alpha[NeighbourCells[i][j]] = step; 
       More=1; 
      } 
      j++; 
    } 
} 
return(More); 
} 

/**********************************************************************************/ 

bool AlphaTime::count(long Lmax, long step) 
{ 
bool err=0; 
const long EndStep=step-1; 
const char Lmax3 = cube(Lmax, Lmax, Lmax); 
long i; 

time = (long*) calloc((size_t) EndStep, sizeof(long)); 
if((alpha==NULL)||(time==NULL)) {err=1; goto RET;} 
for (i = 0; i < EndStep; i++) time[i]=0; 

for (i = 0; i < Lmax3; i++) 
{ 
     time[alpha[i]]++; 
} 

RET: 
return(err); 
} 

/**********************************************************************************/ 

bool AlphaTime::print(long Conc, long Lmax, long step) 
{ 
bool err=0; 
const long EndStep=step-1; 
const char Lmax3 = cube(Lmax, Lmax, Lmax); 
char fname[40]; 
long i, sum=0, Sum; 
FILE *fp; 

sprintf(fname,"alpha_cubeL%ldC%ld", Lmax,Conc); 
fp=fopen(fname,"w"); 

if(fp==(NULL)) goto RET; 
fprintf(fp,"# max time is %ld\n",EndStep); 
fprintf(fp,"# time dalpha alpha\n"); 

for (i = 0, Sum = 0; i < EndStep; i++) 
{ 
     Sum+=time[i]; 
} 
for (i = 0; i < EndStep; i++) 
{ 
     sum+=time[i]; 
     fprintf(fp," %ld  %.5lf %.5lf\n",i+1, (double)time[i]/(double)Sum,(double)sum/(double)Sum); 
} 
fclose(fp); 

sprintf(fnameLN,"LNalpha_cubeL%ldC%ld", Lmax,Conc); 
fp=fopen(fnameLN,"w"); 

if(fp==(NULL)) goto RET; 
fprintf(fp,"# max time is %ld\n",EndStep); 
fprintf(fp,"# ln(t) ln(ln(1/(1-alpha)))\n"); 

for (sum=0, i = 0; i < EndStep; i++) 
{ 
     sum+=time[i]; 

     if ((time[i])&&(sum!=Sum)) 
      fprintf(fp," %5lf %5lf\n", 
        log(i+1), log(log(1/(1-(double)sum/(double)Sum)))); 
} 
fclose(fp); 



RET: 
return(err); 
} 

と出力されます...

> g++ -g -lm avrm.cpp -o avrm 
> avrm 512 30 
Conc = 512, Lmax = 30 
step = 34 
*** glibc detected *** avrm: free(): invalid next size (normal): 0x0811e698 *** 
======= Backtrace: ========= 

...

阿仁のアイデア?お願いします?

+6

「小さな、非常に簡単なプログラム」という興味深い定義があります。問題を絞り込んで投稿し、コードの掲載量を制限する必要があります。 – adelphus

+0

まあ、私がfclose(fp)をコメントするだけであれば。入り口それはすべて正常に動作し、私は持っている他の出力のための別のポインタを追加することができますが、これは正しいことではなく、それを行う適切な方法ではありません。 そして、実際の原因 - その問題の原因がわからないので、私はコード全体を投稿しました。この方法で "fclose"の使用法は違法ではないと確信しています。エラーはどこか別のものでなければなりません...しかし残りは動作しているか、とにかく動作しているようです... –

+3

C++プログラムの中でなぜcallocとfopen/fcloseを使用していますか?新しい[]、iostream/fileストリームを使用してビジネスをさせてください:) – Eregrith

答えて

1

あなたは、配列NeighbourCellsのためだけ7の要素に割り当てられている:

NeighbourCells = (long**) calloc((size_t) 7, sizeof(long*)); 

をしかし、あなたは、配列の末尾を超えてアクセスしている:として

for (row=0; row<(cube(Lmax,Lmax,Lmax)); row++) 
{ 
    NeighbourCells[row]=a1+row*7; 

あなたLmaxで渡された2番目のコマンドライン引数(後longに変換する)。あなたのサンプルランでは、それは30です。したがって、ループカウンタは0から27000cube(30,30,30))になります。

+0

私のプログラムを見て、それについて考えていただきありがとうございます! (Lmax * Lmax * Lmax)X7行X列の2次元配列を割り当てます。 私がこの方法で使用する理由は、 "++"の武器は "新しい"か "ベクトル"のように... –

関連する問題