Millemila
Millemila

Reputation: 1660

Fortran pointer and their memory usage

I know that a C pointer is "a variable that holds an address." What about Fortran pointers? Fortran pointers can either be allocated by pointing them to a target or by using the allocate statement. Which is the memory usage of the pointer in both cases? Lets say that I have a pointer:

pb => b(101:200)

is it correct to say that pb is only taking 32 bit of memory (if compiled on a 32 bit executable) for storing the address of b(101) and other 32 bit to store the number of elements (100)? And If I have:

pb => b(5,1:10)

I guess also the stride has to be stored, so the pointer takes 3*32 bits total, is it correct? But when I allocate a Pointer with:

Allocate(pb(1:100))

am I actually reserving 100 memory locations for that pointer? Is here that I am confused. Can anybody clarify?

thanks Alberto

Upvotes: 2

Views: 1203

Answers (1)

Francois Jacq
Francois Jacq

Reputation: 1274

A fortran array in general (not only a pointer) may be associated to a descriptor containing the address (32 or 64 bits), the lower and upper bounds and the strides. Such descriptor is often used when passing an array by argument to a procedure waiting for an assumed shape array.

When one allocates directly a variable declared pointer, this pointer is associated together to the descriptor above and its associated array.

From a general point of view, allocating directly a pointer is not advised (but this is sometimes necessary for linked lists for instance). I usually allocate only allocatable arrays, and I use pointers only to point to memory zones already allocated.

Allocatable variables have been designed to never involve memory leaks!

Fortran pointers behave diffently than C pointers. You must understand that a Fortran pointer is in fact an alias of the associated memory it points to (alias instead of pointer would have been a better name).

Upvotes: 1

Related Questions