PetscMalloc1#

Allocates an array of memory aligned to PETSC_MEMALIGN

Synopsis#

#include <petscsys.h>
PetscErrorCode PetscMalloc1(size_t m1,type **r1)

Not Collective

Input Parameter#

  • m1 - number of elements to allocate (may be zero)

Output Parameter#

  • r1 - memory allocated

Note#

This uses sizeof() of the memory type requested to determine the total memory to be allocated; therefore, you should not multiply the number of elements requested by the sizeof() the type. For example, use

  PetscInt *id;
  PetscMalloc1(10,&id);

not

  PetscInt *id;
  PetscMalloc1(10*sizeof(PetscInt),&id);

Does not zero the memory allocated, use PetscCalloc1() to obtain memory that has been zeroed.

The PetscMalloc[N]() and PetscCalloc[N]() take an argument of type size_t! However, most codes use value, computed via int or PetscInt variables. This can overflow in 32bit int computation - while computation in 64bit size_t would not overflow! It’s best if any arithmetic that is done for size computations is done with size_t type - avoiding arithmetic overflow!

PetscMalloc[N]() and PetscCalloc[N]() attempt to work-around this by casting the first variable to size_t. This works for most expressions, but not all, such as

  PetscInt *id, a, b;
  PetscMalloc1(use_a_squared ? a * a * b : a * b, &id); // use_a_squared is cast to size_t, but a and b are still PetscInt
  PetscMalloc1(a + b * b, &id); // a is cast to size_t, but b * b is performed at PetscInt precision first due to order-of-operations

These expressions should either be avoided, or appropriately cast variables to size_t#

  PetscInt *id, a, b;
  PetscMalloc1(use_a_squared ? (size_t)a * a * b : (size_t)a * b, &id); // Cast a to size_t before multiplication
  PetscMalloc1(b * b + a, &id); // b is automatically cast to size_t and order-of-operations ensures size_t precision is maintained

See Also#

PetscFree(), PetscNew(), PetscMalloc(), PetscCalloc1(), PetscMalloc2()

Level#

beginner

Location#

include/petscsys.h

Examples#

src/tao/unconstrained/tutorials/burgers_spectral.c
src/tao/tutorials/ex3.c
src/tao/pde_constrained/tutorials/hyperbolic.c
src/tao/complementarity/tutorials/blackscholes.c
src/tao/pde_constrained/tutorials/elliptic.c
src/tao/unconstrained/tutorials/spectraladjointassimilation.c
src/tao/pde_constrained/tutorials/parabolic.c
src/tao/unconstrained/tutorials/minsurf1.c
src/tao/unconstrained/tutorials/minsurf2.c
src/tao/complementarity/tutorials/minsurf1.c


Index of all Sys routines
Table of Contents for all manual pages
Index of all manual pages