Can you post the complete code that causes segfault?
I REPEAT: I AM USING "NUMERICAL RECIPES IN C" FUNCTIONS.
THESE FUNCTIONS ARE MEANT FOR UNIT-OFFSET. i.e.
INSTEAD OF STARTING THE INDEX OF 1D ARRAY FROM [0] ( OR FOR 2D [0][0]), THEY WILL START INDEX FROM [1] FOR 1D ARRAY AND FROM [1][1] FOR 2 D ARRAY.
vector( ) corresponds to 1D
matrix( ) corresponds to 2D
I do not know what is happening inside these two functions; I have never tried that.
Many times these routines (from Numerical Recipes) are used considering them as black boxes.
This is my whole code (nrutil.c contains 2 functions mentioned above):
Code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include"nrutil.c"
#define size 100
#define nl 1
#define nh 10
float ran1(long *idum); // this is random number generator from Numerical Recipe
int main()
{
long *idum;
int i,k,r;
long j;
float n1,n2,n3;
float *dx, *dy, *shift,**sensor_matrix;
shift=vector(1,size);
dx=vector(nl,nh);
dy=vector(nl,nh);
sensor_matrix=matrix(nl,nh,nl,nh);
//________________ dx _________________________
j=-5;
idum = &j;
for(i=1;i<=size;i++)
{
n1=ran1(idum);
n2=n1-0.5;
n3=n2*10.0;
dx[i]=n3;
}
//__________________ dy ________________________
j=-9;
idum = &j;
for(i=1;i<=size;i++)
{
n1=ran1(idum);
n2=n1-0.5;
n3=n2*10.0;
dy[i]=n3;
}
//___________________ shift _____________________
for(i=1;i<=size;i++)
{
shift[i]=sqrtf( powf(dx[i],2.0) + powf(dy[i],2.0) );
}
//___________________ Sensor Matrix ______________
k=1;
for(i=nl;i<=nh;i++)
{
for(r=nl;r<=nh;r++)
{
sensor_matrix[i][r]=shift[k]; // I get seg. fault here
k++;
}
}
//_________________________________________________
free_matrix(sensor_matrix,nl,nh,nl,nh);
free_vector(dx,nl,nh);
free_vector(dy,nl,nh);
free_vector(shift,1,size);
return 0;
}
Regards,
iamc