OpenVMS Pascal constant is not constant when used as size initializer

I think the easiest way to demonstrate the problem is with an example. Code:

PROGRAM CONSTANTSTRING(OUTPUT);

CONST
    C_MaxLength = 30;

VAR
    small_string : VARYING[5] OF CHAR VALUE 'alpha';

PROCEDURE LocalProc(
    localstring : VARYING[C_MaxLength] of CHAR
);
BEGIN
    writeln('localstring length: ', localstring.LENGTH);
    writeln('localstring size: ', SIZE(localstring.BODY));
    writeln('C_MaxLength: ', C_MaxLength);
END;

BEGIN
    writeln('small_string length: ', small_string.LENGTH);
    writeln('small_string size: ', SIZE(small_string.BODY));
    writeln('C_MaxLength: ', C_MaxLength);

    LocalProc(small_string);
END.

      

Compilation:

>pascal /version
HP Pascal I64 V6.1-116 on OpenVMS I64 V8.4
>pascal constantstringinit
>link constantstringinit
>run constantstringinit

      

And the output is:

small_string length:          5
small_string size:          5
C_MaxLength:         30
localstring length:          5
localstring size:          5
C_MaxLength:          5

      

As you can see, the value C_MaxLength

has changed locally within the procedure LocalProc

. Which is odd since it was declared constant.

The new value of the constant is only within the procedure LocalProc

. Code executed in the main menu after the call LocalProc

will use the original value of the constant.

At first it looked like a compiler error to me, but I reasoned that this compiler has been around long enough for something like this to be discovered and recorded or documented. But I can't find any documentation on this. It doesn't help, which VARYING

is an extension of HP, which means I can't compare to other Pascal implementations.

Do any gurus know more about what's going on here?

+3


source to share


1 answer


It's been a very long time and I can't find any documentation to support it, but I think it's a special use case varying[] of char

as a parameter type:

    localstring : VARYING[C_MaxLength] of CHAR

      

This not only declares a parameter localstring

, but also a local environment constant that takes the size of the actual string that passed. It is only because you named it the same as your global constant, which is causing confusion. You haven't actually changed the value C_MaxLength

. Instead, you have another one C_MaxLength

in the local area.



Trying to change this line to something like:

    localstring : VARYING[foo] of CHAR

      

and then browse foo

as well C_MaxLength

. I expect you to see which foo

is 5 and C_MaxLength

is 30.

+6


source







All Articles