Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran pointer attribute to type declarations

I have the following type

  type OCTREE_TYPE
     real                          :: box(2,3)
     integer, allocatable          :: icells(:)
     integer                       :: num_point
     integer                       :: depth
     type(OCTREE_TYPE), pointer    :: parent      => NULL()
     type(OCTREE_TYPE), pointer    :: children(:) => NULL()
  end type OCTREE_TYPE

Now, when I declare a variable of this data-type, what is the difference between this declaration

   type(OCTREE_TYPE),pointer        :: octree_node

and this

  type(OCTREE_TYPE)        :: octree_node

besides that one of them is a pointer and the other is not.

like image 794
ATK Avatar asked Apr 27 '26 08:04

ATK


1 Answers

what is the difference (...) besides that one of them is a pointer and the other is not.

Err... actually, that is the only difference between them: one is a pointer variable, the other is a normal variable. Any other difference derives from such fact. If you are already comfortable with how pointer variables work in Fortran, this answer stops here. In case you're not, here is a brief description.

Variables in Fortran are named references to addresses in memory that contains values, that conform in type and characteristics to those in the variable's declaration. When you operate on a variable, its corresponding value in storage is fetched or updated.

With normal variables, this relation is fixed. The system allocates contiguous storage for its value (and all its components, for a derived type) before it could possibly be used, and release that storage when they wouldn't be used anymore, according to its own criteria. Meanwhile, that variable always references that same address in memory, that is fixed in location and size.

On the other hand, for pointer variables, this relation is dynamic. The system does not allocate any storage for its underlying value automatically, the only space a pointer occupies is an integer variable, that will sotre the address that it is currently pointing to. You have 2 ways to make use of it:

1) you can indicate another variable and make your pointer reference the same address in memory that the other variable references. This is called pointer association, and makes your pointer variable to work as an alias to that other variable. Pointers in Fortran, unlike in other languages, are typed, and this is awesome. You will only be able to associate a pointer to a variable that conforms with it in type, kind and rank, and also if it is marked with the target attribute (or is also a pointer). You can change the target of your pointer whenever you want.

2) you can ask the system to allocate a new storage in memory for this variable with the allocate statement. This is desirable when you want to control when the storage will be allocated or deallocated and fit it to your program needs. Also, you can defer details of the variable to be decided at runtime, like the size/bounds of an array or len parameters of a parameterized type. Be aware that when you choose this way, you are in charge of the management of its memory. If you changed your reference to another address without deallocating the previous one, there may be no other reference to that memory and nor you or the system will be able to free it, and memory leaks will happen.

So, to sum up and answer: with the pointer attribute, you can use this variable to point to and operate other variables, without requiring memory allocation, acd change the target as you want OR you can manage memory for this variable manually, deferring its size, bounds and length to runtime.


Said that, I must remind you of the compromises that comes with the flexibility pointers bring to you. There are lots of gotchas and subtleties when handling pointers, such as memory leaks, dangling pointers, access violation, aliasing..., that could generate errors or performance loss. If the only thing you want for pointers is the dynamic memory allocation part, and don't plan to change its target, you should use the allocatable attribute instead. Allocatable variables and components also have dynamic memory, but the system will manage the deallocation for you, although you can also deallocate manually.

like image 197
Rodrigo Rodrigues Avatar answered Apr 29 '26 23:04

Rodrigo Rodrigues



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!