[petsc-users] Check Vec or Mat for validity

Jed Brown jedbrown at mcs.anl.gov
Tue Oct 2 06:14:12 CDT 2012


On Tue, Oct 2, 2012 at 5:25 AM, Johannes Huber <johannes.huber at unibas.ch>wrote:

>  Hi,
> I'd like to check, whether for a Vec or a Mat the creation process
> (VecCrteate, VecSetSize, VecSetFromOptions or VecDuplicate or ...) has
> already been done or not, i.e. I'd like to check, wether the Vec or Mat is
> a valid object.
>

You can't check for something like VecCreate because the statement

Vec X;

does not initialize X, thus X _could_ point to valid memory (even possibly
containing a valid classid, for example, if the last stack frame had
allocated a vector and malloc has not given the memory back to the system
and has not reused it yet), though it may also point to some garbage
address that cannot be dereferenced without seg-faulting your application.
You could, as a matter of practice, always write

Vec X = PETSC_NULL;

in which case you could check

if (!X) { /* X has not be created yet */ }

but this would be a convention specific to your code.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20121002/b099340c/attachment.html>


More information about the petsc-users mailing list