Nullify

Statement Purpose

The nullify statement is used to disassociate pointers from targets.

Examples and Rules of Use

This statement's use is pretty basic. It is used to break a pointers association with a target. When used it takes the following form.
		Nullify(p,q,x)
Take note that any variable that appears in between the parenthesis of a nullify statement must have been declared as pointer in an earlier type statement and should have been associated prior to use. However, be a little cautious when using this statement. Its use can result in large blocks of memory space being left inaccessible to the program. For instance, if you try to nullify a pointer array that was created with an allocate statement the memory space that the pointer was associated with will become inaccessible to the program unless that memory space has another pointer also associated with it. In fact, this problem can also occur with any type pointer that was dynamically created. To illustrate this, all of the pointers in the following piece of code will leave memory space inaccessible when they are nullified.
		program memoryloss
		implicit none
		real, pointer, dimension(:,:) :: p1
		integer, pointer :: q1
 		character, pointer :: c1*10
		allocate ( p1(5,5), c1, q1)
		c1 = 'example   '
		q1 = 5
		nullify (p1,q1,c1)
One way to prevent memory lose would have been to associate another pointer with the allocated one (e.g. ps1 => p1) before the NULLIFY statement. If the memory allocated to a pointer is really no longer needed, then the best option is to replace the NULLIFY statement with a DEALLOCATE statement. In this instance the DEALLOCATE both frees the extra allocation of memory and nullifies the connection of the pointers to this space. Use of the associated intrinsic function after either the NULLIFY or DEALLOCATE will return a value of false for the nullified or deallocated pointers.

Even though you could cause yourself problems through the careless use of the nullify statement, there are times you absolutely need to use it. The nullify statement is necessary when trying to deallocate pointer arrays that were not created through the use of an allocate statement. For example, in the next few lines of code an allocatable array called green is created and then the pointer p2 is set to point at this array. Later, when an attempt is made to deallocate p2, the computer will actually try to deallocate the array green. In the case of our compiler, this results in an error message, due to the indirect attempt at deallocation.
		program wrong_array
		implicit none
		real, allocatable :: green(:,:)
		real, pointer, dimension(:,:) :: p2
		allocate green(6,7)
		P2 => green
		deallocate (p2)
To prevent the problems that this can cause, replace the DEALLOCATE statement with
		nullify (p2)
.

For More Information and Examples See

lecture forty two

example: associated.f

Up one level / Home


Written by Jason Wehr: jcw142@psu.edu and Maintained by John Mahaffy : jhm@cac.psu.edu