Frank Heckenbach a écrit:
Pascal Viandier wrote:
I have to say, I understand your explanation but not the fundamental meaning of it. By inspecting the local variable with gdb, I see you are right. But since I declared the global string variable as a String(40), I expected the capacity passed to the procedure to be 40, not 3, which is the actual length, not the total string capacity . However, I think this is a rather strange behaviour. Why the original string capacity is not passed along with the value? In fact, this means the local string is not the same type as the one I declared globally and passed by value. Am I wrong?
That's right -- and that's a general property of value parameters. E.g., if you declare an integer subrange variable and pass it to an integer parameter (or vice versa), or an integer variable to a real parameter, or a char to a string parameter, the actual parameter will not have the same type as the formal parameter.
Indeed, in this case it's more surprising, but actually, the alternative would be surprising in even more subtle ways. Then, e.g., these two calls could produce different results:
var a: String (10);
procedure p (s: String); begin ... end;
begin a := 'foo'; p (a); p ('foo'); end.
This would clearly violate the properties of value parameters.
You are right. Still I would consider as "more natural" the rule: with a value String parameter (without explicit capacity), - copy the original string with its capacity if it has one: p(a) - supply capacity = length if not : p('foo')
"more natural" means that I would be able to understand directly the surprising behaviour observed originally by Pascal, without having to ask to somebody who knows so well the internals of gpc.
Maurice