Pointer to PROTECTED variable

Pointer to PROTECTED variable

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?

7 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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.

Retired 12/31/2016

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.

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.

Retired 12/31/2016

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?

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.

Retired 12/31/2016

Thank you so much, Steve!

Leave a Comment

Please sign in to add a comment. Not a member? Join today