By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,025 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Seg Fault on 2nd Pass

P: n/a
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
Share this Question
Share on Google+
14 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.