Segmentation fault Core dump

Pages: 12
ok,

I add these two class to my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

class Submat
{
public:

void nrerror(const char error_text[])
/* Numerical Recipes standard error handler */


{
        fprintf(stderr,"Numerical Recipes run-time error...\n");
        fprintf(stderr,"%s\n",error_text);
        fprintf(stderr,"...now exiting to system...\n");
        exit(1);
}

float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
       long newrl, long newcl)
/* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */


{
        long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
        float **m; 

        /* allocate array of pointers to rows */


        //float **m= new float*[(size_t) ((nrow+NR_END))];
        m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));

       
        if (!m) nrerror( "allocation failure in submatrix()");
         
        m += NR_END; //recently comment
        m -= newrl; //recently comment
             
  
        /* set pointers to rows */
  
       for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;

        /* return pointer to array of pointers to rows */
        return m;
}


void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch)
/* free a submatrix allocated by submatrix() */
{
        free((FREE_ARG) (b+nrl-NR_END));
}
};



class ludc
{
public:

float *vector2(long  nl, long  nh) //vector changed in vector2 to avoid consusion
/* allocate a float vector with subscript range v[nl..nh] */

{
        float *v;

        //v= new float[(size_t) ((nh-nl+1+NR_END))];
        v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
        if (!v) nrerror("allocation failure in vector()");
        return v-nl+NR_END;
}


void free_vector(float *v, long nl, long nh)
 /* free a float vector allocated with vector() */
{
        free((FREE_ARG) (v+nl-NR_END));
}










int ludcmp(float **a, int n, int *indx, float *d)
{
	int i,imax,j,k;
	float big,dum,sum,temp;
	float *vv;

	vv=vector2(1,n);
	*d=1.0;
	for (i=1;i<=n;i++) {
		big=0.0;
		for (j=1;j<=n;j++)
			if ((temp=fabs(a[i][j])) > big) big=temp;
		if (big == 0.0) return 1; //nrerror("Singular matrix in routine ludcmp");
		vv[i]=1.0/big;
	}
	for (j=1;j<=n;j++) {
		for (i=1;i<j;i++) {
			sum=a[i][j];
			for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
			a[i][j]=sum;
		}
		big=0.0;
		for (i=j;i<=n;i++) {
			sum=a[i][j];
			for (k=1;k<j;k++)
				sum -= a[i][k]*a[k][j];
			a[i][j]=sum;
			if ( (dum=vv[i]*fabs(sum)) >= big) {
				big=dum;
				imax=i;
			}
		}
		if (j != imax) {
			for (k=1;k<=n;k++) {
				dum=a[imax][k];
				a[imax][k]=a[j][k];
				a[j][k]=dum;
			}
			*d = -(*d);
			vv[imax]=vv[j];
		}
		indx[j]=imax;
		if (a[j][j] == 0.0) a[j][j]=TINY;
		if (j != n) {
			dum=1.0/(a[j][j]);
			for (i=j+1;i<=n;i++) a[i][j] *= dum;
		}
	}
	free_vector(vv,1,n);
	return 0;
}


void lubksb(float **a, int n, int *indx, float b[])
{
        int i,ii=0,ip,j;
        float sum;

        for (i=1;i<=n;i++) {
                ip=indx[i];
                sum=b[ip];
                b[ip]=b[i];
                if (ii)
                        for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
                else if (sum) ii=i;
                b[i]=sum;
        }
        for (i=n;i>=1;i--) {
                sum=b[i];
                for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
                b[i]=sum/a[i][i];
        }
}
};


How can I call them in the function float levenberg_marquardt_nf_nv?

Without the classes I used to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
float  **alpha, **alpha_shift;
 


              alpha = new float*[nba];
              
                for(i=0;i<nba;i++)
          {
            alpha[i] = new float[nba]; //so we can access the pointer alpha[i][j] (2d allocation)
     
          }


float *beta=  new float[nba];
 int *indx= new int[nba];

 alpha_shift.submatrix(alpha,0,nba-1,0, nba-1,1,1); 

ludcmp(alpha_shift, nba, indx-1, &d);
lubksb(alpha_shift, nba, indx-1, beta-1);

free_submatrix(alpha_shift,1,nba,1, nba);
Last edited on
Hey I solve my problem, I used to have an unitialize pointer pass as argument.
 
float** covar= new float*; 

 
levenberg_marquardt_nf_nv( YY, ZZ, lamb2, sigma2, nbz,  nbf, a, ecarti, ecarts,  nba,  tol, covar,  max_iter, somme1, somme2);


and in levenberg_marquardt_nf_nv :

1
2
                **covar = szo/(nbf*nbz);              
                printf("covar=%e\n",*covar[0]);


valgrind is powerful to track memory leak.

Thanks!!!

Last edited on
Topic archived. No new replies allowed.
Pages: 12