【求助】c语言free 产生的错误
写了一段程序,实在找不出错误,在windows编译(gcc)有时可以运行,有时提示程序停止。索性在linux下编译(gcc)没有问题,可以运行,但结果有错误提示,看不懂是什么原因,求大侠解释。已经知道是在哪个函数里出的问题,程序太大,就把这个函数贴出来
void rml(double ** A, double *y, double **z, double *x, double *v,
int nobs, int xcol, int nran, int *rlev, int * rind, double tol)
{
int i, j, l, iter, flag;
double vary;
int *k = (int *)malloc(sizeof(int)*(nran+1));
double * V = (double *)malloc(sizeof(double)*nobs*(nobs+1)/2);
double * P = (double *)malloc(sizeof(double)*nobs*(nobs+1)/2);
double * vsi = (double *)malloc(sizeof(double)*nobs*(nobs+1)/2);
double * vsj = (double *)malloc(sizeof(double)*nobs*(nobs+1)/2);
double * iyp = (double *)malloc(sizeof(double)*nobs);
double * iypv = (double *)malloc(sizeof(double)*nobs);
double * jyp = (double *)malloc(sizeof(double)*nobs);
double * jypv = (double *)malloc(sizeof(double)*nobs);
double * ai = (double *)malloc(sizeof(double)*(nran+1)*(nran+2)/2);
double * s = (double *)malloc(sizeof(double)*(nran+1));
double * d = (double *)malloc(sizeof(double)*(nran+1));
double * old = (double *)malloc(sizeof(double)*(nran+1));
// initial value
vary=var(y, nobs);
for (i=0; i<nran+1; i++) old[i]=vary/(nran+1);
flag = 0;
iter = 0;
while(flag==0)
{
iter++;
bigV(V, A, z, old, nobs, nran, rlev, rind);
i = ssgj(V, nobs);
bigP(P, V, x, nobs, xcol);
for (i=0; i<nran; i++)
{
ZAZ(z[i], A[i], nobs, rlev[i], vsi, rind[i]);
mult43(y, P, 1, nobs, iyp);
mult43(iyp, vsi, 1, nobs, iypv);
mult43(iypv, P, 1, nobs, iyp);
trmul(iyp, y, 1, nobs, 1, &s[i]);
s[i] = s[i] - PVtrace2(P,vsi,nobs);
for (j=0; j<=i; j++)
{
if (i==j)
{
mult43(iyp, vsi, 1, nobs, jyp);
}
else
{
ZAZ(z[j], A[j], nobs, rlev[j], vsj, rind[j]);
mult43(iyp, vsj, 1, nobs, jyp);
}
mult43(jyp, P, 1, nobs, jypv);
trmul(jypv, y, 1, nobs, 1, &ai[i*(i+1)/2+j]);
//ai[j*(nran+1)+i] = ai[i*(nran+1)+j];
}
mult43(iyp, P, 1, nobs, iypv);
trmul(iypv, y, 1, nobs, 1, &ai[nran*(nran+1)/2+i]);
//ai[nran*(nran+1)+i] = ai[i*(nran+1)+nran];
}
mult43(y, P, 1, nobs, iyp);
mult43(iyp, P, 1, nobs, iypv);
trmul(iypv, y, 1, nobs, 1,&s[nran]);
for (i=0; i<nobs; i++)
{
s[nran] = s[nran] - P[i*(i+1)/2+i];
}
mult43(iypv, P, 1, nobs, iyp);
trmul(iyp, y, 1, nobs, 1, &ai[nran*(nran+1)/2+nran]);
for (i=0; i<(nran+1)*(nran+2)/2; i++) ai[i]=0.5*ai[i];
for (i=0; i<(nran+1); i++) s[i]=0.5*s[i];
i = ssgj(ai, nran+1);
mult34(ai, s, nran+1, 1, d);
for (i=0; i<nran+1; i++)
{
v[i] = old[i]+d[i];
if (v[i]<0.0) v[i] = 0.0;
}
for (i=0; i<nran+1; i++) k[i]=(fabs(v[i]-old[i])< tol)?1:0;
flag=(isum(k,nran+1)==nran+1)?1:0;
matcpy(old, v, nran+1);
printf("Iter %d", iter);
for (i=0; i<nran+1; i++)
{
printf("\t%f", v[i]);
}
printf("\n");
}
free(P); free(vsi); free(vsj); free(iyp); free(iypv); free(jyp); free(jypv); free(d); free(s); free(k); free(ai); free(V);
}
LINUX下的运行错误提示:
Iter 1 0.000000 0.903635 0.277812
Iter 2 0.057224 1.422965 0.140668
Iter 3 0.124686 1.685234 0.059673
Iter 4 0.165416 1.690274 0.062702
Iter 5 0.174786 1.694930 0.061014
Iter 6 0.175191 1.694954 0.060997
Iter 7 0.175193 1.694976 0.060989
Iter 8 0.175193 1.694972 0.060990
Iter 9 0.175193 1.694973 0.060990
Iter 10 0.175193 1.694973 0.060990
*** glibc detected *** ./ai: free(): invalid pointer: 0x0000000002599930 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3509675296]
./ai[0x4027d8]
./ai[0x402ff3]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x350961ecdd]
./ai[0x400659]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:11 493882252 /mnt/disk1/lihd/ai
00603000-00604000 rw-p 00003000 08:11 493882252 /mnt/disk1/lihd/ai
02598000-025b9000 rw-p 00000000 00:00 0 [heap]
3509200000-3509220000 r-xp 00000000 08:01 1048977 /lib64/ld-2.12.so
350941f000-3509420000 r--p 0001f000 08:01 1048977 /lib64/ld-2.12.so
3509420000-3509421000 rw-p 00020000 08:01 1048977 /lib64/ld-2.12.so
3509421000-3509422000 rw-p 00000000 00:00 0
3509600000-3509786000 r-xp 00000000 08:01 1048978 /lib64/libc-2.12.so
3509786000-3509986000 ---p 00186000 08:01 1048978 /lib64/libc-2.12.so
3509986000-350998a000 r--p 00186000 08:01 1048978 /lib64/libc-2.12.so
350998a000-350998b000 rw-p 0018a000 08:01 1048978 /lib64/libc-2.12.so
350998b000-3509990000 rw-p 00000000 00:00 0
3509a00000-3509a83000 r-xp 00000000 08:01 1048995 /lib64/libm-2.12.so
3509a83000-3509c82000 ---p 00083000 08:01 1048995 /lib64/libm-2.12.so
3509c82000-3509c83000 r--p 00082000 08:01 1048995 /lib64/libm-2.12.so
3509c83000-3509c84000 rw-p 00083000 08:01 1048995 /lib64/libm-2.12.so
350f600000-350f616000 r-xp 00000000 08:01 1049014 /lib64/libgcc_s-4.4.6-20110824.so.1
350f616000-350f815000 ---p 00016000 08:01 1049014 /lib64/libgcc_s-4.4.6-20110824.so.1
350f815000-350f816000 rw-p 00015000 08:01 1049014 /lib64/libgcc_s-4.4.6-20110824.so.1
2b9c45472000-2b9c45475000 rw-p 00000000 00:00 0
2b9c45486000-2b9c45488000 rw-p 00000000 00:00 0
7fff1e483000-7fff1e499000 rw-p 00000000 00:00 0 [stack]
7fff1e5ff000-7fff1e600000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
跪求各位大侠帮忙!