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

Matthew Knepley knepley at gmail.com
Mon Dec 21 17:06:36 CST 2009

On Mon, Dec 21, 2009 at 5:01 PM, Umut Tabak <u.tabak at tudelft.nl> wrote:

> 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

Without seeing the code, we cannot say anything, but I bet you misdeclared


> 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
> ...
> private:
> 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);
> MatSetType(A,MATSEQAIJ);
> "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.
> Umut

What most experimenters take for granted before they begin their experiments
is infinitely more interesting than any results to which their experiments
-- Norbert Wiener
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20091221/e5d15380/attachment.htm>

More information about the petsc-users mailing list