[fpc-devel] Question on constref

Bart bartjunk64 at gmail.com
Wed Feb 1 11:30:37 CET 2023


Hi,

I have a function that has a parameter to a datastructure with some
arrays with records is it.
This function needs to return a pointer to a specific record inside
that structure.
The function is not supposed to alter the contents of this datastructure.
So, I need to pass this structure by reference.
(I've spent hours and hours looking at unexpected results in my
program because I passed the datastructure by value, so the returned
pointer was in fact garbage...)

I thought that constref would be OK for that (the word constref
suggests to me tah the paramter will be treated (by me) to be a
constant, and that it shall be passed by reference in all cases,
whereas with a const parameter the compiler decides upon the best way
to pass it: by value or by reference).
I tried to find documentation for constref, but all I could find was:
https://wiki.freepascal.org/FPC_New_Features_2.6.0#Constref_parameter_modifier
There it says:
"Note that in general, it should only be used for interfacing with
external code or when writing assembler routines."

I therefore decided to make it a var parameter, but that feels a bit
odd since the function shall not ever alter the datastructure itself.
Hence my question: is it OK to use constref for a parameter (and is it
guaranteed to work by design this way) if I need to pas a parameter by
reference and the function/procedure shall not modify that parameter?

(B.t.w.: Where can I find the official documentation on constref?)

-- 
Bart


More information about the fpc-devel mailing list