Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a Fortran POINTER require a TARGET?

Why does the Fortran 90 Specification specify (5.2.8) that the TARGET keyword must be used to associate a POINTER to it? Why isn't every type a valid TARGET?

For example,

INTEGER, POINTER :: px
INTEGER, TARGET :: x
x = 5
px => x
is valid syntax but
INTEGER, POINTER :: px
INTEGER :: x
x = 5
px => x
is not valid.

Why must this be?

like image 533
Steve Avatar asked Jan 18 '11 23:01

Steve


People also ask

What is target in Fortran?

A target is another normal variable, with space set aside for it. A target variable must be declared with the target attribute. You associate a pointer variable with a target variable using the association operator (=>).

Why use pointers in Fortran?

Pointers are a new feature to the Fortran standard and bring Fortran 90 into line with languages like C. The use of pointers can provide: A flexible alternative to allocatable arrays. The tools to create and manipulate dynamic data structures (such as linked lists).

What is pointer attribute in Fortran?

The POINTER attribute designates objects as pointer variables. The term pointer refers to objects with the Fortran 90 POINTER attribute.

What is the point of pointers in C?

The Pointer in C, is a variable that stores address of another variable. A pointer can also be used to refer to another pointer function. A pointer can be incremented/decremented, i.e., to point to the next/ previous memory location. The purpose of pointer is to save memory space and achieve faster execution time.


2 Answers

An item that might be pointed to could be aliased to another item, and the compiler must allow for this. Items without the target attribute should not be aliased and the compiler can make assumptions based on this and therefore produce more efficient code.

like image 169
M. S. B. Avatar answered Oct 03 '22 18:10

M. S. B.


Pointers in fortran are different than pointers in c. In fortran 90 pointers were provided with few restriction like having a target. This was done to address speed issue and to keep pointer usage safe. Although one call make allocatable pointers which do not need to specify a target. Dig deeper and you will find them!!

like image 29
sukhbinder Avatar answered Oct 03 '22 17:10

sukhbinder