473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Seg Fault on 2nd Pass

Hello. I'm after some general advice. At the moment i have some code
that should run a FOR loop a set number of times. Each time the
structure stamps is read in as follows:

for( i = 0 ; i < stamp_number ; i++ )
{
printf("%g\n",stamps[1].scprod[2]);
....

Now on the first pass when i = 0 the whole loop runs fine. However
onthe 2nd pass, I get a Seg Fault when the printf line above is read.
Now as you can see the value read from stamps is not varying with the
FOR loop parameter but is the same both times, but somehow the second
time the same line is read (when the values in stmaps have not been
altered by the rest of the code) I get a Seg Fault.

Therefore, I'm just after some ideas regarding how this is possible?

Kind Regards,

Matt

Aug 13 '07 #1
14 2141
Matt <ma*****@hotmail.comwrites:
Hello. I'm after some general advice. At the moment i have some code
that should run a FOR loop a set number of times. Each time the
structure stamps is read in as follows:

for( i = 0 ; i < stamp_number ; i++ )
{
printf("%g\n",stamps[1].scprod[2]);
...

Now on the first pass when i = 0 the whole loop runs fine. However
onthe 2nd pass, I get a Seg Fault when the printf line above is read
Now as you can see the value read from stamps is not varying with the
FOR loop parameter but is the same both times, but somehow the second
time the same line is read (when the values in stmaps have not been
altered by the rest of the code) I get a Seg Fault.

Therefore, I'm just after some ideas regarding how this is possible?
The most likely reason is simply that some code in the loop is messing
up you program's data. For example, undefined access though a rouge
pointer could change 'stamps' so that stamps[1] becomes invalid as
triggers the fault. The real problem is almost always somewhere else.

If your program is not large, try to cut it down into a postable
example of the problem. If it is large, it should already be made up
of small, testable, pieces. Provoke the error with a test program
that just exercises the piece that is causing the trouble and post a
cut-down version of that!

--
Ben.
Aug 14 '07 #2

"Matt" <ma*****@hotmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
Hello. I'm after some general advice. At the moment i have some code
that should run a FOR loop a set number of times. Each time the
structure stamps is read in as follows:

for( i = 0 ; i < stamp_number ; i++ )
{
printf("%g\n",stamps[1].scprod[2]);
...

Now on the first pass when i = 0 the whole loop runs fine. However
onthe 2nd pass, I get a Seg Fault when the printf line above is read.
Now as you can see the value read from stamps is not varying with the
FOR loop parameter but is the same both times, but somehow the second
time the same line is read (when the values in stmaps have not been
altered by the rest of the code) I get a Seg Fault.

Therefore, I'm just after some ideas regarding how this is possible?
Something else you are doing later in the loop is causing the problem,
probably clobbering stamps or its members.
Show the whole code.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
Aug 14 '07 #3
On Aug 14, 1:06 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Matt <matt...@hotmail.comwrites:
Hello. I'm after some general advice. At the moment i have some code
that should run a FOR loop a set number of times. Each time the
structure stamps is read in as follows:
for( i = 0 ; i < stamp_number ; i++ )
{
printf("%g\n",stamps[1].scprod[2]);
...
Now on the first pass when i = 0 the whole loop runs fine. However
onthe 2nd pass, I get a Seg Fault when the printf line above is read
Now as you can see the value read from stamps is not varying with the
FOR loop parameter but is the same both times, but somehow the second
time the same line is read (when the values in stmaps have not been
altered by the rest of the code) I get a Seg Fault.
Therefore, I'm just after some ideas regarding how this is possible?

The most likely reason is simply that some code in the loop is messing
up you program's data. For example, undefined access though a rouge
pointer could change 'stamps' so that stamps[1] becomes invalid as
triggers the fault. The real problem is almost always somewhere else.

If your program is not large, try to cut it down into a postable
example of the problem. If it is large, it should already be made up
of small, testable, pieces. Provoke the error with a test program
that just exercises the piece that is causing the trouble and post a
cut-down version of that!

--
Ben.
Rhe smal testable pieces are in the many seperate source files I have.
Sadly they are all heavily inter-dependant and many lines o fcode are
executed in the main function before this particular function is
reached. If it is of any use, I've posted the FOR loop in its entirety
below so you can see if there are any changes, though I should note
that the only relevant one I can find is "stamps[i].norm = sum;" b ut
this is changing a different sub-structure to scprod.

I've commented the GSL parts so it is clear what each part is doing.

for( i = 0 ; i < stamp_number ; i++ )

{

printf("%g\n",stamps[1].scprod[2]); /* SEGMENTATION FAULT HERE ON
2ND PASS FROM i DO LOOP */

for( im = 1 ; im <= ncomps ; im++ )

{

gsl_vector_set( check_vec , im , stamps[i].scprod[im] ); /* Sets
check_vec[im] = stamps[i].scprod[im] */

for( jm = 1 ; jm <= im ; jm++ )

{

gsl_matrix_set( check_mat , im , jm , stamps[i].mat[im]
[jm] ); /* Sets check_mat[im][jm] = stamps[i].mat[im][jm] */

}

}

gsl_matrix_view check_mat_view = gsl_matrix_view_array
( check_mat , ncomp_total + 1 + 100 , ncomp_total + 1 + 100 ); /*
Create a vector view */
gsl_vector_view check_vec_view = gsl_vector_view_array
( check_vec , ncomp_total + 1 + 100 ); /* Create a matrix view */

gsl_linalg_LU_decomp( &check_mat_view.matrix , indx , &d ); /*
Factorise square matrix check_mat_view into LU decomposition PA=LU */

gsl_linalg_LU_svx( &check_mat_view.matrix , indx ,
&check_vec_view.vector ); /* Solve the square system */

for( j = 0 , sum = 0.0 ; j < ncomp_kernel ; j++ )

{

sum += gsl_vector_get( check_vec , j + 1 ) * sum_vectors[j]; /*
Read check_vec[j+1] */

}

check_stack[i] = sum;
stamps[i].norm = sum;
mean += sum;
sigma += sum * sum;

printf("x: %i y: %i %lf\n", stamps[i].x , stamps[i].y , sum );

}
Kind Regards,

Matt
Aug 14 '07 #4
Matt <ma*****@hotmail.comwrites:
On Aug 14, 1:06 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>Matt <matt...@hotmail.comwrites:
Hello. I'm after some general advice. At the moment i have some code
that should run a FOR loop a set number of times. Each time the
structure stamps is read in as follows:
for( i = 0 ; i < stamp_number ; i++ )
{
printf("%g\n",stamps[1].scprod[2]);
...
Now on the first pass when i = 0 the whole loop runs fine. However
onthe 2nd pass, I get a Seg Fault
<snip>
>The most likely reason is simply that some code in the loop is messing
up you program's data.
<snip>
>If your program is not large, try to cut it down into a postable
example of the problem. If it is large, it should already be made up
of small, testable, pieces. Provoke the error with a test program
that just exercises the piece that is causing the trouble and post a
cut-down version of that!
Rhe smal testable pieces are in the many seperate source files I have.
Sadly they are all heavily inter-dependant and many lines o fcode are
executed in the main function before this particular function is
reached. If it is of any use, I've posted the FOR loop in its entirety
below so you can see if there are any changes, though I should note
that the only relevant one I can find is "stamps[i].norm = sum;" b ut
this is changing a different sub-structure to scprod.
C does not works like that. Any one of the gsl_* functions could be
messing up your data if it is passed the wrong stuff. Even the line
'check_stack[i] = sum;' could be at fault depending on the definition
of check_stack. You probably know this. The important point is that
debugging requires fanatical skepticism -- everything is in doubt
until you are sure about it.

How to move forward? You should try to cut down the code. Without
knowing the problem domain, I can't say how much sense this makes, but
removing chucks of the loop might let you find the problem.

You should consider using compile options like -fmudflap (if you are
using gcc) and tools like valgrind. Even configuring malloc to check
for some errors might help.

Also, I note that there is a lively help list for gsl[2], but try
really hard to make a cut-down example. They will want one as much as
we do! BTW, if *have* to post a code fragment, always include at
least the declarations of the main variables involved.

<code snipped>

[1] MALLOC_CHECK_=2 with most gcc libraries.
[2] http://lists.gnu.org/archive/html/help-gsl/

--
Ben.
Aug 14 '07 #5
On Aug 14, 12:37 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Matt <matt...@hotmail.comwrites:
On Aug 14, 1:06 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Matt <matt...@hotmail.comwrites:
Hello. I'm after some general advice. At the moment i have some code
that should run a FOR loop a set number of times. Each time the
structure stamps is read in as follows:
for( i = 0 ; i < stamp_number ; i++ )
{
printf("%g\n",stamps[1].scprod[2]);
...
Now on the first pass when i = 0 the whole loop runs fine. However
onthe 2nd pass, I get a Seg Fault
<snip>
The most likely reason is simply that some code in the loop is messing
up you program's data.
<snip>
If your program is not large, try to cut it down into a postable
example of the problem. If it is large, it should already be made up
of small, testable, pieces. Provoke the error with a test program
that just exercises the piece that is causing the trouble and post a
cut-down version of that!
Rhe smal testable pieces are in the many seperate source files I have.
Sadly they are all heavily inter-dependant and many lines o fcode are
executed in the main function before this particular function is
reached. If it is of any use, I've posted the FOR loop in its entirety
below so you can see if there are any changes, though I should note
that the only relevant one I can find is "stamps[i].norm = sum;" b ut
this is changing a different sub-structure to scprod.

C does not works like that. Any one of the gsl_* functions could be
messing up your data if it is passed the wrong stuff. Even the line
'check_stack[i] = sum;' could be at fault depending on the definition
of check_stack. You probably know this. The important point is that
debugging requires fanatical skepticism -- everything is in doubt
until you are sure about it.

How to move forward? You should try to cut down the code. Without
knowing the problem domain, I can't say how much sense this makes, but
removing chucks of the loop might let you find the problem.

You should consider using compile options like -fmudflap (if you are
using gcc) and tools like valgrind. Even configuring malloc to check
for some errors might help.

Also, I note that there is a lively help list for gsl[2], but try
really hard to make a cut-down example. They will want one as much as
we do! BTW, if *have* to post a code fragment, always include at
least the declarations of the main variables involved.

<code snipped>

[1] MALLOC_CHECK_=2 with most gcc libraries.
[2]http://lists.gnu.org/archive/html/help-gsl/

--
Ben.
Thanks for the reply.

As requested, here is the section of the code in question. It will
compile, but it is now seg faulting on the first attempt I make to
assign a value to stamps[1].scprod[2]. I'm assuming there is a link
between this and the actual problem.

/* A programme to test a seg fault error I'm getting */

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_linalg.h>

typedef struct

{
int x , y;
double **vectors;
double *area;
double **mat;
double *scprod;
double sum;
double chi2;
double norm;
double diff;
char keep;
} stamp_struct;

stamp_struct *stamps;

int main()

{

int i,j;
int im, jm;
int d;

double mean, sigma, sum, *sum_vectors, *check_stack;

stamps = (stamp_struct *)malloc( 196 * sizeof(stamp_struct) );

stamps[i].scprod = (double *)malloc( 55 * sizeof(double) );

gsl_permutation * indx = gsl_permutation_calloc( 425 );

mean = sigma = 0.0;

printf("Seg_Fault_Test.c: Matrix 'check_mat' declared with size %d
\n" , 425 );
gsl_matrix *check_mat = gsl_matrix_calloc( 425 , 425 );

printf("Seg_Fault_Test.c: Matrix 'check_vec' declared with size %d
\n" , 425 );
gsl_vector *check_vec = gsl_vector_calloc( 425 );

stamps[1].scprod[2] = 10.0;

for( i = 0 ; i < 96 ; i++ )

{

printf("Seg_Fault_Test.c: Begin Pass %d\n",i);

printf("%g\n",stamps[1].scprod[2]); /* SEGMENTATION FAULT HERE ON
2ND PASS FROM i DO LOOP */

printf("Seg_Fault_Test.c: Statement printed successfully\n");

for( im = 1 ; im <= 54 ; im++ )

{

gsl_vector_set( check_vec , im , stamps[i].scprod[im] );

for( jm = 1 ; jm <= im ; jm++ )

{

gsl_matrix_set( check_mat , im , jm , stamps[i].mat[im][jm] );

}

}

gsl_matrix_view check_mat_view = gsl_matrix_view_array
( check_mat , 425 , 425 );
gsl_vector_view check_vec_view = gsl_vector_view_array
( check_vec , 425 );

gsl_linalg_LU_decomp( &check_mat_view.matrix , indx , &d );

gsl_linalg_LU_svx( &check_mat_view.matrix , indx ,
&check_vec_view.vector );

for( j = 0 , sum = 0.0 ; j < 53 ; j++ )

{

sum += gsl_vector_get( check_vec , j + 1 ) * sum_vectors[j];

}

check_stack[i] = sum;
stamps[i].norm = sum;
mean += sum;
sigma += sum * sum;

printf("x: %i y: %i %lf\n", stamps[i].x , stamps[i].y , sum );

}

return 0;

}

Kind Regards,

Matt

Aug 14 '07 #6
In article <11**********************@57g2000hsv.googlegroups. com>,
Matt <ma*****@hotmail.comwrote:
>Hello. I'm after some general advice. At the moment i have some code
that should run a FOR loop a set number of times. Each time the
structure stamps is read in as follows:

for( i = 0 ; i < stamp_number ; i++ )
{
printf("%g\n",stamps[1].scprod[2]);
...

Now on the first pass when i = 0 the whole loop runs fine. However
onthe 2nd pass, I get a Seg Fault when the printf line above is read.
Run your program under a debugger. Set a breakpoint at the printf
line. When it stops there, set it to stop when the value of
stamps[1].scprod[2] changes. This will show you where the value
is getting messed up.

How you tell the debugger to stop when the value changes depends on
your debugger.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 14 '07 #7
Matt wrote:
As requested, here is the section of the code in question. It will
compile, but it is now seg faulting on the first attempt I make to
assign a value to stamps[1].scprod[2]. I'm assuming there is a link
between this and the actual problem.
<snip>
int main()

{

int i,j;
int im, jm;
int d;

double mean, sigma, sum, *sum_vectors, *check_stack;

stamps = (stamp_struct *)malloc( 196 * sizeof(stamp_struct) );

stamps[i].scprod = (double *)malloc( 55 * sizeof(double) );
i is uninitialized here. Goodness only knows where you've put the
address of the block of doubles...

(You've also not checked the results of malloc())

Aug 14 '07 #8
Matt <ma*****@hotmail.comwrites:
Thanks for the reply.

As requested, here is the section of the code in question. It will
compile, but it is now seg faulting on the first attempt I make to
assign a value to stamps[1].scprod[2]. I'm assuming there is a link
between this and the actual problem.

/* A programme to test a seg fault error I'm getting */

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_linalg.h>

typedef struct

{
int x , y;
double **vectors;
double *area;
double **mat;
double *scprod;
double sum;
double chi2;
double norm;
double diff;
char keep;
} stamp_struct;

stamp_struct *stamps;

int main()

{

int i,j;
int im, jm;
int d;

double mean, sigma, sum, *sum_vectors, *check_stack;

stamps = (stamp_struct *)malloc( 196 * sizeof(stamp_struct) );

stamps[i].scprod = (double *)malloc( 55 * sizeof(double) );
^
i is indeterminate.
gsl_permutation * indx = gsl_permutation_calloc( 425 );

mean = sigma = 0.0;

printf("Seg_Fault_Test.c: Matrix 'check_mat' declared with size %d
\n" , 425 );
gsl_matrix *check_mat = gsl_matrix_calloc( 425 , 425 );

printf("Seg_Fault_Test.c: Matrix 'check_vec' declared with size %d
\n" , 425 );
gsl_vector *check_vec = gsl_vector_calloc( 425 );

stamps[1].scprod[2] = 10.0;

for( i = 0 ; i < 96 ; i++ )

{

printf("Seg_Fault_Test.c: Begin Pass %d\n",i);

printf("%g\n",stamps[1].scprod[2]); /* SEGMENTATION FAULT HERE ON
2ND PASS FROM i DO LOOP */

printf("Seg_Fault_Test.c: Statement printed successfully\n");

for( im = 1 ; im <= 54 ; im++ )

{

gsl_vector_set( check_vec , im , stamps[i].scprod[im] );

for( jm = 1 ; jm <= im ; jm++ )

{

gsl_matrix_set( check_mat , im , jm , stamps[i].mat[im][jm]
);
For this, and the vector set line above, stamps[i].scprod and .mat
must all have been allocated and set.
}

}

gsl_matrix_view check_mat_view = gsl_matrix_view_array
( check_mat , 425 , 425 );
gsl_vector_view check_vec_view = gsl_vector_view_array
( check_vec , 425 );

gsl_linalg_LU_decomp( &check_mat_view.matrix , indx , &d );

gsl_linalg_LU_svx( &check_mat_view.matrix , indx ,
&check_vec_view.vector );

for( j = 0 , sum = 0.0 ; j < 53 ; j++ )

{

sum += gsl_vector_get( check_vec , j + 1 ) * sum_vectors[j];

}

check_stack[i] = sum;
stamps[i].norm = sum;
mean += sum;
sigma += sum * sum;

printf("x: %i y: %i %lf\n", stamps[i].x , stamps[i].y , sum );

}

return 0;

}
I did in fact correct these errors. The program then reports an error
inside gsl_linalg_LU_decomp. My next action would have been to reduce
the data set size but here I hit a snag. It is not wise to have all
these "magic numbers" peppering your code. There is probably a fixed
and logical relationship between 196, 96, 55, 425, 54 etc. and what I
wanted to find was a:

#define DATA_SIZE_SIZE 55

(or maybe a couple of these) that I could change and have everything
else change in relation to it. When (if?) you post to the gsl help
list, you should have a program that can be altered in this way.

I think you will get better help from the gsl people.

--
Ben.
Aug 14 '07 #9
On Aug 14, 1:56 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
Matt wrote:
As requested, here is the section of the code in question. It will
compile, but it is now seg faulting on the first attempt I make to
assign a value to stamps[1].scprod[2]. I'm assuming there is a link
between this and the actual problem.

<snip>
int main()
{
int i,j;
int im, jm;
int d;
double mean, sigma, sum, *sum_vectors, *check_stack;
stamps = (stamp_struct *)malloc( 196 * sizeof(stamp_struct) );
stamps[i].scprod = (double *)malloc( 55 * sizeof(double) );

i is uninitialized here. Goodness only knows where you've put the
address of the block of doubles...

(You've also not checked the results of malloc())
Bit ashamed I put that out now given the number of mistakes I'm fixing
in it - I'll put up a version closer to what I intended shortly.

Aug 14 '07 #10
On Aug 14, 2:36 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Matt <matt...@hotmail.comwrites:
Thanks for the reply.
As requested, here is the section of the code in question. It will
compile, but it is now seg faulting on the first attempt I make to
assign a value to stamps[1].scprod[2]. I'm assuming there is a link
between this and the actual problem.
/* A programme to test a seg fault error I'm getting */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_linalg.h>
typedef struct
{
int x , y;
double **vectors;
double *area;
double **mat;
double *scprod;
double sum;
double chi2;
double norm;
double diff;
char keep;
} stamp_struct;
stamp_struct *stamps;
int main()
{
int i,j;
int im, jm;
int d;
double mean, sigma, sum, *sum_vectors, *check_stack;
stamps = (stamp_struct *)malloc( 196 * sizeof(stamp_struct) );
stamps[i].scprod = (double *)malloc( 55 * sizeof(double) );

^
i is indeterminate.
gsl_permutation * indx = gsl_permutation_calloc( 425 );
mean = sigma = 0.0;
printf("Seg_Fault_Test.c: Matrix 'check_mat' declared with size %d
\n" , 425 );
gsl_matrix *check_mat = gsl_matrix_calloc( 425 , 425 );
printf("Seg_Fault_Test.c: Matrix 'check_vec' declared with size %d
\n" , 425 );
gsl_vector *check_vec = gsl_vector_calloc( 425 );
stamps[1].scprod[2] = 10.0;
for( i = 0 ; i < 96 ; i++ )
{
printf("Seg_Fault_Test.c: Begin Pass %d\n",i);
printf("%g\n",stamps[1].scprod[2]); /* SEGMENTATION FAULT HERE ON
2ND PASS FROM i DO LOOP */
printf("Seg_Fault_Test.c: Statement printed successfully\n");
for( im = 1 ; im <= 54 ; im++ )
{
gsl_vector_set( check_vec , im , stamps[i].scprod[im] );
for( jm = 1 ; jm <= im ; jm++ )
{
gsl_matrix_set( check_mat , im , jm , stamps[i].mat[im][jm]
);

For this, and the vector set line above, stamps[i].scprod and .mat
must all have been allocated and set.
}
}
gsl_matrix_view check_mat_view = gsl_matrix_view_array
( check_mat , 425 , 425 );
gsl_vector_view check_vec_view = gsl_vector_view_array
( check_vec , 425 );
gsl_linalg_LU_decomp( &check_mat_view.matrix , indx , &d );
gsl_linalg_LU_svx( &check_mat_view.matrix , indx ,
&check_vec_view.vector );
for( j = 0 , sum = 0.0 ; j < 53 ; j++ )
{
sum += gsl_vector_get( check_vec , j + 1 ) * sum_vectors[j];
}
check_stack[i] = sum;
stamps[i].norm = sum;
mean += sum;
sigma += sum * sum;
printf("x: %i y: %i %lf\n", stamps[i].x , stamps[i].y , sum );
}
return 0;
}

I did in fact correct these errors. The program then reports an error
inside gsl_linalg_LU_decomp. My next action would have been to reduce
the data set size but here I hit a snag. It is not wise to have all
these "magic numbers" peppering your code. There is probably a fixed
and logical relationship between 196, 96, 55, 425, 54 etc. and what I
wanted to find was a:

#define DATA_SIZE_SIZE 55

(or maybe a couple of these) that I could change and have everything
else change in relation to it. When (if?) you post to the gsl help
list, you should have a program that can be altered in this way.

I think you will get better help from the gsl people.

--
Ben.
After correcting many silly mistakes, I've got the test program to now
execute without any problems and have included it below in case anyone
is interested. Hopefully by making the same changes to my main program
the seg faults situation will be resolved.

/* A programme to test a seg fault error I'm getting */

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_linalg.h>

typedef struct

{
int x , y;
double **vectors;
double *area;
double **mat;
double *scprod;
double sum;
double chi2;
double norm;
double diff;
char keep;
} stamp_struct;

stamp_struct *stamps;

int main()

{

int i , j;
int im;
int d;
int a = 0;
int b = 0;
int matrix_size = 425;
int stamps_size = 196;
int scprod_size = 55;
int sum_num = 53;
int check_vec_size = 54;

double mean, sigma, sum, *sum_vectors, *check_stack;

stamps = (stamp_struct *)malloc( stamps_size *
sizeof(stamp_struct) );

for ( i = 0 ; i < stamps_size ; i++ )

{

stamps[i].scprod = (double *)malloc( scprod_size *
sizeof(double) );

}

gsl_permutation *indx = gsl_permutation_calloc( matrix_size );

mean = sigma = 0.0;

printf("Seg_Fault_Test.c: Matrix 'check_mat' declared with size %d
\n" , matrix_size );
gsl_matrix *check_mat = gsl_matrix_calloc( matrix_size ,
matrix_size );

printf("Seg_Fault_Test.c: Matrix 'check_vec' declared with size %d
\n" , matrix_size );
gsl_vector *check_vec = gsl_vector_calloc( matrix_size );

/* Give initial values to stamps[i].scprod[j] */

for( i = 0 ; i < stamps_size ; i++ )

{

for ( j = 0 ; j < scprod_size ; j++ )

{

stamps[i].scprod[j] = a;
a = ( rand() % 1000 );

}

}

/* Give initial values to check_mat */

for( i = 0 ; i < matrix_size ; i++ )

{

for ( j = 0 ; j < matrix_size ; j++ )

{

b = ( rand() % 100000 );
gsl_matrix_set( check_mat, i , j , b );

}

}

for( i = 0 ; i < stamps_size ; i++ )

{

printf("Seg_Fault_Test.c: Begin Pass %d\n" , i );

printf("stamps[1].scprod[2] = %g\n" , stamps[1].scprod[2] );

printf("Seg_Fault_Test.c: Statement printed successfully\n");

for( im = 1 ; im <= check_vec_size ; im++ )

{

gsl_vector_set( check_vec , im , stamps[i].scprod[im] );

}

/* gsl_matrix_view check_mat_view = gsl_matrix_view_array
( check_mat , matrix_size , matrix_size );
gsl_vector_view check_vec_view = gsl_vector_view_array
( check_vec , matrix_size ); */

printf("Seg_Fault_Test.c: Test 2\n");
printf("check_mat[1][2] = %g\n" , gsl_matrix_get( check_mat , 1 ,
2 ));

/* gsl_linalg_LU_decomp( &check_mat_view.matrix , indx , &d );
Segmentation fault here */

gsl_linalg_LU_decomp( check_mat , indx , &d ); /* Segmentation
fault here */

printf("Seg_Fault_Test.c: Test 3\n");

gsl_linalg_LU_svx( check_mat , indx , check_vec );

for( j = 0 , sum = 0.0 ; j < sum_num ; j++ )

{

sum += gsl_vector_get( check_vec , j + 1 ) * sum_vectors[j];

}

check_stack[i] = sum;
stamps[i].norm = sum;
mean += sum;
sigma += sum * sum;

printf("x: %i y: %i %lf\n", stamps[i].x , stamps[i].y , sum );

}

return 0;

}

Kind Regards,

Matt

Aug 14 '07 #11
Matt <ma*****@hotmail.comwrites:
After correcting many silly mistakes, I've got the test program to now
execute without any problems and have included it below in case anyone
is interested. Hopefully by making the same changes to my main program
the seg faults situation will be resolved.
<snip>
double mean, sigma, sum, *sum_vectors, *check_stack;
<snip>
sum += gsl_vector_get( check_vec , j + 1 ) * sum_vectors[j];
sum_vectors is not initialised. I suggest you compile and run with
-fmudfalp next time. I suspect that, when the C errors are removed,
you will have a question that does not belong here, but it may take a
while to get there!

--
Ben.
Aug 14 '07 #12
On Aug 14, 4:17 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Matt <matt...@hotmail.comwrites:
After correcting many silly mistakes, I've got the test program to now
execute without any problems and have included it below in case anyone
is interested. Hopefully by making the same changes to my main program
the seg faults situation will be resolved.

<snip>
double mean, sigma, sum, *sum_vectors, *check_stack;
<snip>
sum += gsl_vector_get( check_vec , j + 1 ) * sum_vectors[j];

sum_vectors is not initialised. I suggest you compile and run with
-fmudfalp next time. I suspect that, when the C errors are removed,
you will have a question that does not belong here, but it may take a
while to get there!

--
Ben.
Thanks for that, though I'm assuming you mean -fmudflap from your
earlier post :)

When I add -fmudflap to the Makefile I get:

cc1: error: mf-runtime.h: No such file or directory

Any idea where this mf-runtime.h should be?

Kind Regards,

Matt

Aug 14 '07 #13
Matt <ma*****@hotmail.comwrites:
On Aug 14, 4:17 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>Matt <matt...@hotmail.comwrites:
After correcting many silly mistakes, I've got the test program to now
execute without any problems and have included it below in case anyone
is interested. Hopefully by making the same changes to my main program
the seg faults situation will be resolved.

<snip>
double mean, sigma, sum, *sum_vectors, *check_stack;
<snip>
sum += gsl_vector_get( check_vec , j + 1 ) * sum_vectors[j];

sum_vectors is not initialised. I suggest you compile and run with
-fmudfalp next time. I suspect that, when the C errors are removed,
you will have a question that does not belong here, but it may take a
while to get there!

Thanks for that, though I'm assuming you mean -fmudflap from your
earlier post :)

When I add -fmudflap to the Makefile I get:

cc1: error: mf-runtime.h: No such file or directory

Any idea where this mf-runtime.h should be?
You need to post to a group about your OS. How to drive your tools is
off topic here.

--
Ben.
Aug 14 '07 #14
On Tue, 14 Aug 2007 14:45:25 -0000, Matt <ma*****@hotmail.comwrote:

stamps = (stamp_struct *)malloc( stamps_size * sizeof(stamp_struct) );
In C, casting the result of malloc is poor style, and in some cases
can mask errors. Also, if the type of 'stamps' changes, then you
would need to modify both the cast and the sizeof. You can avoid both
of those issues by writing

stamps = malloc(stamps_size * sizeof *stamps);

Jim
Aug 15 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jay donnell | last post by:
I have a short multi-threaded script that checks web images to make sure they are still there. I get a segmentation fault everytime I run it and I can't figure out why. Writing threaded scripts is...
6
by: AMT2K5 | last post by:
Hello guys. I have a function, cleanSpace . I was told from another source that the problem is, is that the function is acting on constant string literal. To fix this I was told to take the...
3
by: Juggernaut | last post by:
I have a program that compiles without errors, but when I run it I get a segment fault. I have managed to trace where it happens, but since im new to C I don't know how to fix this or how I did...
6
by: tigrfire | last post by:
I've been working on a program to try and play a game of Craps, based on a version I found elsewhere - I didn't code the original, but I added a few things such as a balance and wager system. I'm...
2
by: Chris Grossbe | last post by:
Hi! this problem has been a headache for about 2 hours now and i just cant figure it out. ive got a c program that connects to a DB via mysql. currently ive got 2 querys. the first worked...
2
by: danielhdez14142 | last post by:
Some time ago, I had a segment of code like vector<vector<int example; f(example); and inside f, I defined vector<int>'s and used push_back to get them inside example. I got a segmentation...
11
by: John Williams | last post by:
I've written a simple program to do XOR encryption as my first foray into understanding how encryption works. The code compiles fine, however it segmentation faults on every run. using gdb to...
4
by: Goran | last post by:
Hi all, i have a problem on program termination. At the moment when main() ends my program creates a segmentation fault. #include <libmylib/myObject.h>
5
by: doni | last post by:
Hi, I am a beginner to C and I wrote a program that takes arguments and prints it in hex. I am getting a segmentation fault if I dont pass any arguments instead I want it to display the Usage...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.