2

我是新来询问有关编程的问题,所以如果我没有以正确的方式/以适当的方式做某事......请原谅我。

我习惯于“简单”的 ANSI C 编程,但决定尝试将一些客观的编程技能添加到我的“口袋”中——现在看起来不太好。起初我无法以任何方式成功使用“new”——在运行时分配一个二维数组,但最后我放弃了,只是像以前一样使用了 calloc。

但是现在我在使用 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: =========

...

阿尼的想法?请?

4

1 回答 1

1

您只7为数组分配了元素NeighbourCells

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

但是您正在访问超出数组端:

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

因为您Lmax是传递的第二个命令行参数(在转换为 long 之后)。在您的示例运行中,它是30. 所以你的循环计数器从0to运行27000(即cube(30,30,30)

于 2012-02-13T10:17:29.727 回答