Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Christoph_F_
Beginner
482 Views

VALGRIND: Invalid read/write of size 8 (Fortran pointers)

Dear Sir or Madame,

I came across a Valgrind error message that is related to the use of Fortran pointers. See below for a minimal testcase.

Compiler: ifort (IFORT) 19.0.5.281 20190815
IntelMPI: Version 2019 Update 6 Build 20191024 (needed for loading the Valgrind module)
Valgrind: valgrind-3.14.0

Running

valgrind ./a.out

produces the error message (among other messages)

==22137== Invalid write of size 8
==22137==    at 0x404659: MAIN__ (test.f:11)
==22137==    by 0x4037D1: main (in /p/home/jusers/friedrich1/jureca/git/spex/tests/valgrind/a.out)
==22137==  Address 0x1ffe859090 is on thread 1's stack
==22137==  in frame #0, created by MAIN__ (test.f:1)
==22137==
==22137== Invalid read of size 8
==22137==    at 0x404354: MAIN__ (test.f:11)
==22137==    by 0x4037D1: main (in /p/home/jusers/friedrich1/jureca/git/spex/tests/valgrind/a.out)
==22137==  Address 0x1ffe859090 is on thread 1's stack
==22137==  in frame #0, created by MAIN__ (test.f:1)

if the program has been compiled in the following way:

ifort -cpp -O0 -g -DALLOC=pointer -DDIM=63 test.f (and any DIM higher than 63)

but NOT if it has been compiled like this:

ifort -cpp -O0 -g -DALLOC=pointer -DDIM=62 test.f (and any DIM smaller than 62)
ifort -cpp -O0 -g -DALLOC=allocatable -DDIM=63 test.f (and any other DIM).

GFORTRAN does not produce this error.

I would like to know if I should worry about this error. Or is there a violation of the Fortran standard in the code?

Thank you.


Best wishes
Christoph


**************
TESTCASE:

      program test

      implicit none
      real(8), ALLOC :: a(:,:,:), b(:,:,:)

      write(*,*) DIM
      allocate ( a(DIM,DIM,DIM) )
      allocate ( b(DIM,DIM,DIM) )
      a = 0
      b = 0
      a = a - b
      deallocate(a)
      deallocate(b)

      end

0 Kudos
8 Replies
Arjen_Markus
Valued Contributor III
476 Views

What is the value of DIM in this test program? I do not see it set. Oh, and there are other errors, like "ALLOC" instead of "ALLOCATABLE". This program can't be correct.

Christoph_F_
Beginner
469 Views

DIM and ALLOC are set by the compilation command (preprocessor), e.g., 

ifort -cpp -O0 -g -DALLOC=pointer -DDIM=63 test.f

So, in this case, DIM would be set as "63" and ALLOC would be set as "pointer".

Christoph

 

 

Arjen_Markus
Valued Contributor III
466 Views

Ah, missed that bit - I scrolled down to the source code :). That explains it.

andrew_4619
Valued Contributor III
464 Views

I have never used -D but reading the help. "Defines a symbol name that can be associated with an optional value. This definition is used during preprocessing in both Intel® Fortran conditional compilation directives and the fpp preprocessor. "

-D It does not define a Fortran variable so I do not think your usage is valid.

Christoph_F_
Beginner
459 Views

No, what "-DDIM=63" simply does is replacing all occurrences of DIM by 63 before compilation. You can also do that by hand and compile without the preprocessor ("-cpp").

andrew_4619
Valued Contributor III
453 Views

OK I guess the the different between allocatable and pointer is heap or stack. You maybe have a stack overflow.

Christoph_F_
Beginner
450 Views

Thank you.

Interesting possibility. I have just checked it: When pointers are allocated, the memory goes to the heap. (I checked the VmStk value of /proc/PID/status.)

andrew_4619
Valued Contributor III
432 Views

Allocated arrays  go on the heap. You might want to read the heap arrays option also or stack size option