Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28381 Discussions

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

Christoph_F_
Beginner
3,178 Views

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
Honored Contributor I
3,172 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.

0 Kudos
Christoph_F_
Beginner
3,165 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

 

 

0 Kudos
Arjen_Markus
Honored Contributor I
3,162 Views

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

0 Kudos
andrew_4619
Honored Contributor II
3,160 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.

0 Kudos
Christoph_F_
Beginner
3,155 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").

0 Kudos
andrew_4619
Honored Contributor II
3,149 Views

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

0 Kudos
Christoph_F_
Beginner
3,146 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.)

0 Kudos
andrew_4619
Honored Contributor II
3,128 Views

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

 

0 Kudos
Reply