[petsc-users] Memory corruption with two-dimensional array and PetscMemzero

Christophe Ortiz christophe.ortiz at ciemat.es
Mon May 19 04:14:10 CDT 2014


On Thu, May 15, 2014 at 2:08 AM, Jed Brown <jed at jedbrown.org> wrote:

> Christophe Ortiz <christophe.ortiz at ciemat.es> writes:
>
> > Hi all,
> >
> > I am experiencing some problems of memory corruption with PetscMemzero().
> >
> > I set the values of the Jacobian by blocks using MatSetValuesBlocked().
> To
> > do so, I use some temporary two-dimensional arrays[dof][dof] that I must
> > reset at each loop.
> >
> > Inside FormIJacobian, for instance, I declare the following
> two-dimensional
> > array:
> >
> >    PetscScalar  diag[dof][dof];
> >
> > and then, to zero the array diag[][] I do
> >
> >    ierr = PetscMemzero(diag,dof*dof*sizeof(PetscScalar));
>
> Note that this can also be spelled
>
>   PetscMemzero(diag,sizeof diag);
>

Ok.


>
> > Then, inside main(), once dof is determined, I allocate memory for diag
> as
> > follows:
> >
> >  diag = (PetscScalar**)malloc(sizeof(PetscScalar*) * dof);
> >
> >  for (k = 0; k < dof; k++){
> >   diag[k] = (PetscScalar*)malloc(sizeof(PetscScalar) * dof);
> >  }
> > That is, the classical way to allocate memory using the pointer notation.
>
> Note that you can do a contiguous allocation by creating a Vec, then use
> VecGetArray2D to get 2D indexing of it.
>


Good to know. I'll try. Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20140519/80878537/attachment.html>


More information about the petsc-users mailing list