为什么Valgrind在PETSc中为char *赋值时报告了大小为8的无效写入?

时间:2016-02-23 20:00:25

标签: c valgrind petsc

我在PETSc中终止了我的申请。我已经检查了Valgrind发生了什么,但我不明白它的报告:

==97331== Invalid write of size 8
==97331==    at 0x10007FED5: PetscHeaderCreate_Private (inherit.c:40)
==97331==    by 0x1013EFE23: TSResilCreate (tsresil.c:525)
==97331==    by 0x10139A877: TSGetResil (ts.c:5252)
==97331==    by 0x10138D17E: TSSetFromOptions (ts.c:421)
==97331==    by 0x100011A47: SolveODE (in ./ex31)
==97331==    by 0x100012E98: main (in ./ex31)
==97331==  Address 0x102dde080 is 1,776 bytes inside a block of size 1,780 alloc'd
==97331==    at 0x100025EA1: malloc (vg_replace_malloc.c:303)
==97331==    by 0x10010E8C1: PetscMallocAlign (mal.c:34)
==97331==    by 0x10011063D: PetscTrMallocDefault (mtr.c:188)
==97331==    by 0x1013EFDA0: TSResilCreate (tsresil.c:525)
==97331==    by 0x10139A877: TSGetResil (ts.c:5252)
==97331==    by 0x10138D17E: TSSetFromOptions (ts.c:421)
==97331==    by 0x100011A47: SolveODE (in ./ex31)
==97331==    by 0x100012E98: main (in ./ex31)

在tsresil.c:525(我在PETSc中的实施):

ierr = PetscHeaderCreate(resil,TSRESIL_CLASSID,"TSResil","Time stepping resilience","TS",comm,TSResilDestroy,TSResilView);CHKERRQ(ierr);

在inherit.c中:

 22: /*
 23:    PetscHeaderCreate_Private - Creates a base PETSc object header and fills
 24:    in the default values.  Called by the macro PetscHeaderCreate().
 25: */
 26: PetscErrorCode  PetscHeaderCreate_Private(PetscObject h,PetscClassId classid,const char class_name[],const char descr[],const char mansec[],
 27:                                           MPI_Comm comm,PetscObjectDestroyFunction destroy,PetscObjectViewFunction view)
 28: {
 29:   static PetscInt idcnt = 1;
 30:   PetscErrorCode  ierr;
 31: #if defined(PETSC_USE_LOG)
 32:   PetscObject     *newPetscObjects;
 33:   PetscInt         newPetscObjectsMaxCounts,i;
 34: #endif

 37:   h->classid               = classid;
 38:   h->type                  = 0;
 39:   h->class_name            = (char*)class_name;
 40:   h->description           = (char*)descr;
 41:   h->mansec                = (char*)mansec;

 ...

PetscObject是指向:

的指针
typedef struct _p_PetscObject {
  ...
  char                 *class_name;    /*  for example, "Vec" */
  char                 *description;
  ...

因此,问题似乎与char* descr的分配有关,但我不明白为什么。大小8会告知指针错误吗?

1 个答案:

答案 0 :(得分:1)

Valgrind看起来很抱怨,因为你只为PetscObject分配了1780个字节,这个8字节的写入(从字节1776开始)足以写入你没有分配的内存。

您需要增加分配的大小,或者增加结构的大小,具体取决于完整的分配和分配方法。