C++ Deep Copy Object -


i trying deep copy objects , forth. when run gdb, following error after 1 iteration of loop.

program received signal sigsegv, segmentation fault. 0x0804ab96 in dgcpm::dgcpm (this=0x844b760, cur=0x1) @ dgcpm.c:27 27    memcpy(vrcells, cur->vrcells,sizeof(float)*nthetacells); 

i suspect problem has creating "new class," i'm not sure. suggestions?

(note: "_initialize" code calls fortran subroutine sets values in program.)

here run.c main file:

#include "../include/dgcpm.h"  #define particle_num 5  class dgcpm **mallocmodels(int n);  int main(int argc, char *argv[]){    class dgcpm **m;   class dgcpm **cur;    m=mallocmodels(particle_num);//update   for(int t = 0; t < 48; t++){        //update m, , then...       cur = m;       m = (dgcpm**)malloc(sizeof(class dgcpm *)*particle_num);             for(int i=0;i<particle_num;i++){ randomidx = ((double)rand() / ((double)rand_max + 1));     currentidx = find(cumpw,randomidx,particle_num);         m[i] = new class dgcpm(cur[currentidx]);     }       for(int i=0;i<particle_num;i++){     delete cur[i];     }     free(cur);    }     return 0; }   /*============================================================================    mallocmodels - allocate ensemble of models   ============================================================================*/ class dgcpm **mallocmodels(int n){   class dgcpm **m;    m=(class dgcpm **)amjsafemalloc(sizeof(class dgcpm *)*n,               (char *)"mallocmodels:m");    for(int i=0;i<n;i++)     m[i]=new class dgcpm();    return m; }   /*============================================================================   find - return particle index has high probability of having high weight.   ============================================================================*/  int find(float *cumpw, double randomidx, int nm){     /*wrong implementation*/         int index = 0;         flag = 0;     while(flag == 0){         if(cumpw[i] >= randomidx){         flag = 1;         i++;     }         else{         index ++;         }         }     return index; //sometimes, index going number of models, or number of models + 1, out of bounds. /*correct implementation*/     int index = 0;     for(int = 0; < nm-1; i++){     if(cumpw[i] >= randomidx){         index = i;     break;     }     }        if(index >= nm){     index = nm-1;     printf("error: random index exceeds bounds");     }     return index; } 

here dgcpm.h header file:

class dgcpm{  public:   dgcpm(); /* initialized defaults setup */   dgcpm(class dgcpm *cur); //copy constructor   dgcpm(int nthetacells, int nphicells, float thetamin, float thetamax);     ~dgcpm(); /* free memory */  private:   int internal; /* 1=memory allocated internally , should deallocated when ~dgcpm called, 2=memory internal except mgridn external */   int nthetacells,nrcells,nphicells;   float thetamin,thetamax;   float rmin,rmax;   float delr,delphi;   float deltmax;   float *vrcells; /* [nthetacells] */   float *vthetacells; /* [nthetacells] */   float *vphicells; /* [nphicells] */    float **mgridb; /* [nphicells][nthetacells] */   float **mgridbi; /* [nphicells][nthetacells] */   float **mgridpot; /* [nphicells][nthetacells] */   float **mgrider; /* [nphicells][nthetacells] */   float **mgridep; /* [nphicells][nthetacells] */   float **mgridvr; /* [nphicells][nthetacells] */   float **mgridvp; /* [nphicells][nthetacells] */   float **mgridn; /* [nphicells][nthetacells] */   float **mgridhalf; /* [nphicells][nthetacells] particles / weber (workspace upwind , superbee) */   float **mgridden; /* [nphicells][nthetacells] */   float **mgridvol; /* [nphicells][nthetacells] */   float **mgridx; /* [nphicells][nthetacells] */   float **mgridy; /* [nphicells][nthetacells] */   float **mgridoc; /* [nphicells][nthetacells] */   float **std; /* [nphicells][nthetacells] */   float pari[2];    float deltmax;   float re;    void initialize(int nthetacells, int nphicells, float thetamin,            float thetamax);  }; 

and dgcpm.c object wrapper:

/******************************************************************************  * dgcpm.c - implements dgcpm plasmasphere model class               *  ******************************************************************************/ #define two_pi 6.2831853071795864769252866 #include "../include/dgcpm.h" # include <cstdlib> # include <cmath>  /*============================================================================   dgcpm::dgcpm()    initialize default setup   ============================================================================*/ dgcpm::dgcpm(){    internal=1;    initialize(200,200,14.963217,60.0);/*(180,200,14.963217,60.0);*/ }  //copy constructor dgcpm::dgcpm(class dgcpm *cur){    internal=1;    initialize(200,200,14.963217,60.0);/*(180,200,14.963217,60.0);*/    memcpy(vrcells, cur->vrcells,sizeof(float)*nthetacells);   memcpy(vphicells, cur->vphicells,sizeof(float)*nphicells);   memcpy(vthetacells, cur->vthetacells,sizeof(float)*nthetacells);   memcpy(mgridb[0], cur->mgridb[0],sizeof(float)*nthetacells*nphicells);   memcpy(mgridbi[0], cur->mgridbi[0],sizeof(float)*nthetacells*nphicells);   memcpy(mgridpot[0], cur->mgridpot[0],sizeof(float)*nthetacells*nphicells);   memcpy(mgrider[0], cur->mgrider[0],sizeof(float)*nthetacells*nphicells);   memcpy(mgridep[0], cur->mgridep[0],sizeof(float)*nthetacells*nphicells);   memcpy(mgridvr[0], cur->mgridvr[0],sizeof(float)*nthetacells*nphicells);   memcpy(mgridvp[0], cur->mgridvp[0],sizeof(float)*nthetacells*nphicells);   memcpy(mgridn[0], cur->mgridn[0],sizeof(float)*nthetacells*nphicells);   memcpy(mgridhalf[0], cur->mgridhalf[0],sizeof(float)*nthetacells*nphicells);   memcpy(mgridden[0], cur->mgridden[0],sizeof(float)*nthetacells*nphicells);   memcpy(mgridvol[0], cur->mgridvol[0],sizeof(float)*nthetacells*nphicells);   memcpy(mgridoc[0], cur->mgridoc[0],sizeof(float)*nthetacells*nphicells);   memcpy(mgridx[0], cur->mgridx[0],sizeof(float)*nthetacells*nphicells);    memcpy(mgridy[0], cur->mgridy[0],sizeof(float)*nthetacells*nphicells);   memcpy(std[0], cur->std[0],sizeof(float)*nthetacells*nphicells);  }    /*============================================================================   dgcpm::~dgcpm()    free allocated memory   ============================================================================*/ dgcpm::~dgcpm(){   if(internal>=1){     amjfree1dfloat(vrcells);     amjfree1dfloat(vthetacells);     amjfree1dfloat(vphicells);     amjfree2dfloat(mgridb);     amjfree2dfloat(mgridbi);     amjfree2dfloat(mgrider);     amjfree2dfloat(mgridep);     amjfree2dfloat(mgridvr);     amjfree2dfloat(mgridvp);     if(internal==1) amjfree2dfloat(mgridn);     amjfree2dfloat(mgridhalf);     amjfree2dfloat(mgridden);     amjfree2dfloat(mgridvol);     amjfree2dfloat(mgridx);     amjfree2dfloat(mgridy);     amjfree2dfloat(mgridoc);     amjfree2dfloat(std);   } }   /******************************************************************************  ******************************************************************************  **                         private functions                                **  ******************************************************************************  ******************************************************************************/  /*============================================================================   dgcpm::initialize(int nthetacells, int nphicells, float thetamin,            float thetamax);    initialization function used when memory should   allocated internally.   ============================================================================*/ void dgcpm::initialize(int nthetacells, int nphicells, float thetamin,                 float thetamax){    initialize(nthetacells,nphicells,thetamin,thetamax,          amjmalloc1dfloat(nthetacells,(char *)"dgcpm::dgcpm:vrcells"),          amjmalloc1dfloat(nthetacells,(char *)"dgcpm::dgcpm:vthetacells"),          amjmalloc1dfloat(nphicells,(char *)"dgcpm::dgcpm:vphicells"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridb"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridbi"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridpot"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgrider"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridep"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridvr"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridvp"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridn"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridhalf"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridden"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridvol"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridx"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridy"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridoc"),          //added j.wise           amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:std")); } /*============================================================================   dgcpm::initialize(int nthetacells, int nphicells, float thetamin,            float thetamax);    initialization function used when mgridn passed   outside other memory allocated internally.   ============================================================================*/ void dgcpm::initialize(int nthetacells, int nphicells, float thetamin,                 float thetamax, float **mgridn){    initialize(nthetacells,nphicells,thetamin,thetamax,          amjmalloc1dfloat(nthetacells,(char *)"dgcpm::dgcpm:vrcells"),          amjmalloc1dfloat(nthetacells,(char *)"dgcpm::dgcpm:vthetacells"),          amjmalloc1dfloat(nphicells,(char *)"dgcpm::dgcpm:vphicells"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridb"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridbi"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridpot"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgrider"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridep"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridvr"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridvp"),          mgridn,          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridhalf"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridden"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridvol"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridx"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridy"),          amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:mgridoc"),           amjmalloc2dfloat(nphicells,nthetacells,                   (char *)"dgcpm::dgcpm:std"));  }      /*       initialize() - initialization function uses pre-allocated       memory areas passed in outside. function used both       when dgcpm allocates memory , when receives       pre-allocated memory outside in order eliminate       duplication of code associated risk of errors.        ============================================================================*/     void dgcpm::initialize(int nthetacells, int nphicells, float thetamin,                     float thetamax, float *vrcells, float *vthetacells,                    float *vphicells, float **mgridb, float **mgridbi,                     float **mgridpot, float **mgrider, float **mgridep,                    float **mgridvr, float **mgridvp, float **mgridn,                     float **mgridhalf, float **mgridden, float **mgridvol,                    float **mgridx, float **mgridy, float **mgridoc, float **std){        dgcpm::nthetacells=nthetacells;       dgcpm::nphicells=nphicells;       dgcpm::thetamin=thetamin;       dgcpm::thetamax=thetamax;       dgcpm::vrcells=vrcells;       dgcpm::vthetacells=vthetacells;       dgcpm::vphicells=vphicells;       dgcpm::mgridb=mgridb;       dgcpm::mgridbi=mgridbi;       dgcpm::mgridpot=mgridpot;       dgcpm::mgrider=mgrider;       dgcpm::mgridep=mgridep;       dgcpm::mgridvr=mgridvr;       dgcpm::mgridvp=mgridvp;       dgcpm::mgridn=mgridn;       dgcpm::mgridhalf=mgridhalf;       dgcpm::mgridden=mgridden;       dgcpm::mgridvol=mgridvol;       dgcpm::mgridx=mgridx;       dgcpm::mgridy=mgridy;       dgcpm::mgridoc=mgridoc;       dgcpm::std=std;       re=6.378e6;          initialize_(&nthetacells,&nrcells,&nphicells,&thetamin,&thetamax,&rmin,&rmax,               &delr,&delphi,vrcells,vthetacells,vphicells,mgridb[0],mgridbi[0],               mgridn[0],mgridden[0],mgridvol[0],mgridx[0],mgridy[0],mgridoc[0],std[0]);     } 

here's sample custom memory function, takes care of initialization , allocation:

void *amjsafemalloc(int n, char *message){    void *d;    d=malloc(n);    if(d==null){     fprintf(stderr,"amjsafemalloc error: not allocate %d bytes "         "for %s. exiting.\n",n,message);     exit(1);   }    return d; }   float *amjmalloc1dfloat(int a, char *message){    float *d;    sprintf(msg,"%s:amjmalloc1dfloat:d",message);   d=(float *)amjsafemalloc(sizeof(float)*a,msg);    return d; }  float **amjmalloc2dfloat(int a, int b, char *message){    float **d;   int i;    sprintf(msg,"%s:amjmalloc2dfloat:d",message);   d=(float **)amjsafemalloc(sizeof(float *)*a,msg);   sprintf(msg,"%s:amjmalloc2dfloat:d[0]",message);   d[0]=(float *)amjsafemalloc(sizeof(float)*a*b,msg);    for(i=1;i<a;i++) d[i]=d[i-1]+b;    return d; } 

class dgcpm { public:     dgcpm(int nthetacells, int nphicells)     : nthetacells(nthetacells)     , nphicells(nphicells)     , mgridb(nthetacells, vector<float>(nphicells)) // first y x     {     }  private:     int nthetacells, nphicells;     vector<vector<float>> mgridb; }; 

deep copies free. deletes memory free.

by free mean don't have write code..


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -