It sounds like the reason is that a dynamic array is essentially just a pointer, and arr is syntactic sugar to dereference that pointer. In my case, the A record was holding references to dynamic arrays, and it was those array elements that I was able to reassign through a constref parameter, which confused me: A = record In other words, should I use constref whenever I do not reassign a formal parameter and I want to enforce passing a record by reference, or only when that record is also truly immutable in the sense that the procedure does not write to any of its fields either? But also, what is the difference from the perspective of the compiler, since apparently it does not catch the fact that even with constref I am able to mutate fields of a record that is passed by reference?Īs the responder points out, the above sample code should (and does) fail to compile with fpc 3.x I had oversimplified. I was able to compile this just fine under fpc 3.0.4 with either var or constref, but my gut feeling is that rec should probably be declared as var here so as to indicate that the memory occupied by this record instance is changed by the procedure call? More advanced is going into the direction of resources. Create the datastructure runtime from various unlinked arrays is the easiest alternative. But these kind of hacks are not terribly nice. proc does not reassign rec, but it does perform a side-effect in that it mutates fields of its formal parameter. In FPC and D2009+ a pointer to X can be overindexed as an array. This distinction makes sense for simple types such as numbers and strings, but what about structured types? Assuming I have the following record: A = recordĪnd a procedure that acts on As like the following: procedure proc(var rec: A) Following is the declaration of an array of pointers to an integer type iptr integer var parray: array 1. ![]() There may be a situation, when we want to maintain an array, which can store pointers to integers or characters or any other data type available. ![]() var suggests mutation of the argument is possible, while const does not Pascal allows defining an array of pointers.Both var and constref enforce that a parameter is passed by reference Free Pascal supports arrays as in Turbo Pascal.I've been reading about the various means of passing parameters to procedures and functions, but I am still confused about the semantics of var and constref as they apply to records (this is regarding the Free Pascal dialect of the language, v3.x).
0 Comments
Leave a Reply. |