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

Undefined pointer/array *not crashing*

Renaud_Egal
Novice
2,354 Views

Hello everyone,

I'm having issues with this sample of code

subroutine tri_2D(vect)
	real(kind=DP), dimension(:,:),pointer :: vect

	integer :: n, n1, n2
	real(kind=DP), dimension(:), allocatable :: STOCK1, STOCK2
	real(kind=DP) :: temp1,temp2

	integer :: j, jj, jcompt, flag, jk
	
	integer :: nn,nntemp

	integer :: k,w

	integer :: markeur
	real(kind=DP),dimension(:), pointer :: ptr_temp

	! recuperation des dimensions de vect
	n1=size(vect,1)
	n2=size(vect,2)

!	write(unit=*,fmt=*) "n1=",n1,"n2=",n2
!	pause

	if (n2/=2) then
		write(unit=*,fmt=*) "erreur de dimension de vect, tri 2D : dim non égale a 2"
		return
	endif

	nn=n1
	allocate(stock1(nn),stock2(nn))
	
	do k=1,nn
		STOCK1(k)=vect(k,1)
		STOCK2(k)=vect(k,2)
	enddo

.........

Oddly, I declare these variables as "allocatable", so they should be allocated after the statement allocate, right ?

Except I get that "Undefined pointer/aray" (see attached picture). So I guess that it should crash when I affect a value in the "do loop" that follows ? But it doesn't. The debugger still goes through the code

 

Please advise. I can't figure out what's going on in my statements.

Regards,

Renaud Egal

0 Kudos
6 Replies
mecej4
Honored Contributor III
2,354 Views

 I declare these variables as "allocatable", so they should be allocated after the statement allocate, right ?
Not necessarily. You can guard against failed allocation requests by including a STAT= clause in the ALLOCATE statement. If the ALLOCATE did fail, which could happen if nn is large, that would explain why the debugger shows the array as undefined.

You can also use the intrinsic function ALLOCATED to test whether an array has been properly allocated before using the array -- for example, to store information into it.

0 Kudos
Renaud_Egal
Novice
2,354 Views

Ok, I'll try that as soon as I can.

But in this case nn value was 12, not so much large. And if it failed to allocate, wouln't it crash when work is done on it ?

Thanks

0 Kudos
mecej4
Honored Contributor III
2,354 Views

And if it failed to allocate, wouln't it crash when work is done on it ?
That's what I'd expect.

It would help if you could post a complete program source with instructions build and run, and provide information on the compiler version and options used.

0 Kudos
andrew_4619
Honored Contributor III
2,354 Views

I think you need to show the declaration of vect in the calling routine also.

0 Kudos
IanH
Honored Contributor III
2,354 Views

If an allocate statement without a STAT specifier encounters an error, then the program should terminate (see F2008 6.7.1.2p9).

This just looks like the debugger getting confused about where the variable being watched is actually kept in memory, or similar.  This is not unusual.  On its own it may not indicate a problem with the program (or may not be relevant to any other observed problems with the program).

0 Kudos
Renaud_Egal
Novice
2,354 Views

Guys,

from what you said I figured that it was an abnormal functionning of VS, not fortran itself.

Then I reinstalled VS and the problem disappeared. Strange.

Reply