L
lixiaoyao
hi all
I use matrix & vector function to allocate the space myself in c,
typedef struct matrix_array newdata;
struct matrix_array{
float **sy,*sxx;
};
newdata ndata;//new data struct
ndata.sy=matrix(1,nvar,1,nstep);
ndata.sxx=vector(1,nstep);
the question lies there,if I free the space there,it will be ok,but if
I do like this way,
rkdumb(vstart,nvar,x1,x2,nstep,(void *)&ndata,derivefunc);
use void* to deference the data point, and free it after the
function,it will tell me
"Segmentation fault",who can tell me what is the reason?
thanks a lot
float **matrix(long nrl, long nrh, long ncl, long nch)
/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch]
*/
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
float **m;
/* allocate pointers to rows */
m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
float *vector(long nl, long nh)
/* allocate a float vector with subscript range v[nl..nh] */
{
float *v;
v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
if (!v) nrerror("allocation failure in vector()");
return v-nl+NR_END;
}
ndata.sy=matrix(1,nvar,1,nstep);
I use matrix & vector function to allocate the space myself in c,
typedef struct matrix_array newdata;
struct matrix_array{
float **sy,*sxx;
};
newdata ndata;//new data struct
ndata.sy=matrix(1,nvar,1,nstep);
ndata.sxx=vector(1,nstep);
the question lies there,if I free the space there,it will be ok,but if
I do like this way,
rkdumb(vstart,nvar,x1,x2,nstep,(void *)&ndata,derivefunc);
use void* to deference the data point, and free it after the
function,it will tell me
"Segmentation fault",who can tell me what is the reason?
thanks a lot
float **matrix(long nrl, long nrh, long ncl, long nch)
/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch]
*/
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
float **m;
/* allocate pointers to rows */
m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
float *vector(long nl, long nh)
/* allocate a float vector with subscript range v[nl..nh] */
{
float *v;
v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
if (!v) nrerror("allocation failure in vector()");
return v-nl+NR_END;
}
ndata.sy=matrix(1,nvar,1,nstep);