[petsc-users] create a matrix in C++ constructor

Umut Tabak u.tabak at tudelft.nl
Mon Dec 21 17:01:31 CST 2009

Dear all,

I was trying to wrap around some Petsc code into a class, so that there 
is a neat interface and implementation, I got into a problem at the very 
beginning and did not understand the reason maybe some of you could help 
me to understand this.

I have a Matrix A in the private section of my simple class.

And I would like to create this matrix inside the class ctor. Whenever I 
call the MatCreate function I get a segmentation fault with

MatCreate(MPI_COMM_SELF, &A)

I checked the documentation that the Matrices in Petsc are also pointers.

The point I did not understand is that if there is a pointer member of a 
class, say
int* b

 I can initialize that to null in the constructor and make the necessary 
allocation with some member functions. With the same reasoning I thought 
that If I do not apply similar null pointer assignment for the 
matrix(there is not such thing I suppose) that I would like to 
initialize in the ctor, I will get a segfault. I could not figure my way 
out and wondered when you write the topdown code. I mean in the code below,

Mat A;

MatCreate(MPI_COMM_SELF, &A);
MatSetSizes(A, m, n, 0, 0);

"Mat A" initializes the pointer to null internally, am I right? Is there 
sth similar for the above class situation or am I mistaken at some other 
point(which could be highly likely)?

Any pointers are appreciated.

More information about the petsc-users mailing list