Segmentation fault when passing matrix to a function

Hi I am trying to teach my self C++ after using MATLB for so long. I am trying to code up a few bits from Numerical Recipes 3rd ed. I am using their Gaussian elimination function so I am assuming that it is correct. My problem is when I try to pass a Matrix to the gaussj() in gauss.h, I get "segmentation fault" at runtime -- no line number of file etc. I believe this usually means that there is an issue accessing protected memory, so I am assuming I am passing my matrix incorrectly.

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
  #include "nr3.h"
#include "gaussj.h"
#include <stdio.h>

Int main (void)
{

	Int n=3, m = 3;
	MatDoub  a(n,m);//,b(n,m);
	
	a[0][0] = 0;
	a[0][1] = 1;
	a[0][2] = 3;
	a[1][0] = 2;
	a[1][1] = 4;
	a[1][2] = 5;
	a[2][0] = 7;
	a[2][1] = 3;
	a[2][2] = 9;
 	
// for debugging remove later
//n = a[0][1];
//printf("This is a  %d ",n); //Shows the correct value

	gaussj(a); //must be a problem here?
return 0;
}


gaussj() is written by the NR guys, and has two definitions

1
2
void gaussj(MatDoub_IO &a, MatDoub_IO &b)
//Lots of code from here. not shown 


and the overloaded version (still written by the NR guys)

1
2
3
4
5
6
7
8
void gaussj(MatDoub_IO &a)
//Overloaded version with no right-hand sides.  Replaces a by its inverse.
{
//for debuggingremove later
printf("Something ");
	MatDoub b(a.nrows(),0);
	gaussj(a,b);
}


MatDoub_IO is a matrix that the NR guys define in nr3.h

.

Any help on where I am going wrong will be appreciated.



I have no experience with NR (and I'm not going to subscribe just to see their documentation... sorry).

The only thing that I can see that could possibly be a problem is on line 6 of your last example. What does the second argument to MatDoub's constructor represent? If you are constructing a dimensionless matrix perhaps the gaussj() function can't handle that...

That's my best guess. Hope this helps.
Thanks for the follow up. MatDoub is a nother matrix type that they define in NR3.h (not sure the difference between MatDoub or MatDoub_IO). The second example is something that they wrote (I added the printf statement).

I tried to call the function in my main(), in the same way that they do in the second example

I was hoping that my mistake is independent of of the NR stuff. I was hoping that some one might see something like "you have to pass 'a' as a pointer" or something that is C++ specific. PS i tried the pointer thing and that didn't work either.

Can anyone see if I am making a mistake? It is compiling OK. I still cant get this to work...

I may try the GSL libraries next and see if that makes a difference.
You are passing a MatDoub when it is expecting a MatDoub_IO. You need to find out the difference between these two types of Matrix as I suspect this is the issue, even though (I assume) it compiles OK.

If for example MatDoub_IO is derived from MatDoub but with extra members, some compilers will not complain about your code, it will just fall over at runtime when those extra bits it's looking for aren't there.
OK, I changed to using the GSL libraries. Much better! things work now. Thanks for the help
Topic archived. No new replies allowed.