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

Pointer to PROTECTED variable

Espen_M_
Beginner
648 Views

I get the following message when I try to point to a PROTECTED variable from outside of the modele where it is defined:

A use associated object that has the PROTECTED attribute shall not appear as a target in a pointer-assignment-stmt

What is the rationale behind disallowing this?

If I declare a pointer in the module where the PROTECED variable is declared, associate this pointer with the PROTECTED variable, and and then associate the original pointer with the one in the module there is no error. However, now the PROTECTion is also lost and I'm able to reassign the value of the PROTECTED variable.
Is this intensional, ie. standard conforming, behavior, or is it a bug?

0 Kudos
6 Replies
Steven_L_Intel1
Employee
648 Views
The standard says: C551 A nonpointer object that has the PROTECTED attribute and is accessed by use association shall not appear in a variable definition context or as the data-target or proc-target in a pointer-assignment-stmt. The idea of PROTECTED is that use outside the module is restricted, but inside the module it is assumed you know what you are doing.
0 Kudos
Espen_M_
Beginner
648 Views
So if I have a set of objects that I want to be constant but I also want to use them as pointees, what do I do? I cannot use PARAMETERs as they are not even variables... The important thing is that I dont want to accidentally change their values.
0 Kudos
Steven_L_Intel1
Employee
648 Views
Why must they be pointees? Perhaps what you want is to define some access (get) methods that are exposed from the module without exposing the objects themselves. Once you have a pointer to something, you can change its value.
0 Kudos
Espen_M_
Beginner
648 Views
Often a get method will get the job done, but sometimes it will cause unnecessary awkwardness and, more often, performance penalty when passing around "constants" that are function result temporaries instead of pointers. I can use pointers to "ordinary" variables, but ideally I'd like to make them read-only for security reasons. So what about the "security hole" with pointers to pointers to protected variables that I described in the last part of my post? Is this how it is supposed to work?
0 Kudos
Steven_L_Intel1
Employee
648 Views
Yes, that's how it's supposed to work. Within the module, you can do anything you want - and if that's to expose the object through a pointer, then you're free to do that. Since the module is where the PROTECTED attribute was given, it is assumed that the module author knows what they're doing. And in case you wonder - if you make the pointer itself protected, all that means is that the association status of the pointer can't be changed - it doesn't protect what the pointer points to. This was deliberate and argued at length at standards meetings.
0 Kudos
Espen_M_
Beginner
648 Views
Thank you so much, Steve!
0 Kudos
Reply