c++ - memory consumed by array feed function -


i'm beginner in c++, have written code physical problem, code when run consume memory fast. basic idea want function ccjj take array x , i_noise , once don’t want feed them element element. i'm not sure mange array in right way. can 1 tell me thing consume memory , how can correct this? thanks,

#include "ccjjdc_system.h" double *sum_vector(double *a,double *b,double fact){     double *result=new double[2*n];     for(int i=0;i<2*n;i++)         result[i]=a[i]+b[i]*fact;     return result; }  int main(){     file *f;f=fopen("x.dat","w");     double *x=new double[2*n], t, i_noise[n], i=0;     //matrix descrip in paper     //method dinamicaly allocat array      typedef double (a_t)[n];     a_t *a = new a_t[n];     //end of method     //auto a=new double[n][n];     for(int i=0;i<n-1;i++){         a[i][i] = 1.0 + 2.0*alpha;         a[i+1][i] = -alpha;         a[i][i+1] = -alpha;     }     a[n-1][n-1] = 1.0 + 2.0*alpha;     a[0][n-1] = -alpha;     a[n-1][0] = -alpha;     //end of matrix creation     //noise creation     for(int i=0;i<n;i++)         i_noise[i]=0;     //initinal condtion \phi , v     for(int i=0;i<2*n;i++)         x[i]=0;      double          t_max=500,         t_min=0,         h=0.1,         *k1=new double[2*n],         *k2=new double[2*n],         *k3=new double[2*n],         *k4=new double[2*n];     double         i_0=0.0,         i_max=1.0,         di=0.001;        for(i=i_0;i<i_max;i+=di){         for(t=t_min;t<t_max;t=t+h){             k1=ccjjdc(x,t,i_noise,i,a);             k2=ccjjdc(sum_vector(x,k1,h/2),t,i_noise,i,a);             k3=ccjjdc(sum_vector(x,k2,h/2),t,i_noise,i,a);             k4=ccjjdc(sum_vector(x,k3,h),t,i_noise,i,a);             for(int i=0;i<2*n;i++)                 x[i]=x[i]+(k1[i]+(2*k2[i])+(2*k3[i])+k4[i])*h/6;         }         double v=0;         for(int i=0;i<n;i++)             v+=x[i];         std::cout<<"i: "<<i<<"\t v: "<<v<<std::endl;         fprintf(f,"%f\t",i);         fprintf(f,"%f\t",v);         fprintf(f,"\n");         fflush(f);     }     std::cout<<"finish"<<std::endl;     fclose(f);     return 0; } 

and function ccjj

double *ccjjdc(double *x,double t,double *i_noise,double i,double (*a)[n]){     double *xn;     xn=x;     //double         //*result=new double[2*n],         //i_noise[n],         //x[2*n],         //dx[2*n];     for(int i=0;i<n;i++){         for(int j=0;j<n;j++){             x[j]+=a[i][j]*xn[j+n];         }         x[i+n]=i-sin(xn[i])-beta*x[i]+amp*sin(omega*t)+i_noise[i];     }     return x; } 

you should read memory allocation. every time you're using "new" you're allocating memory in c++, requeried free (as opposed languages garbage collection java):

http://en.wikipedia.org/wiki/delete_(c%2b%2b)


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -