Is pointing to a target dummy argument safe after return?

See also the discussion Why/When is the `target` attribute part of the characterisics of a procedure

IMO, one should never use the target attribute for a dummy argument, when the pointer pointing to it is supposed to be valid after the scope had been left. It should work (in theory,) if the actual argument indeed has the target attribute (but only then!), but no compiler I know gives you any warning/error, if you actually forget to specify that attribute for the actual argument. So, sooner or later, it is bound to fail due to oversight (and it can be a mess to find the bug…)

I always specify the pointer attribute for the dummy argument in those cases, as all compilers I know will ensure that the actual argument has either the pointer or the target attribute. So you are safely pass back pointers to them as they are warranted to be valid also after the end of the scope and you won’t be hit by any copy-in/copy-out issues.

4 Likes