Cardinal numbers 
Author Message
 Cardinal numbers

Is the following program legal?  What is displayed?

MODULE N;
FROM InOut IMPORT WriteCard, WriteLn;
PROCEDURE Out(S : ARRAY OF CHAR);
BEGIN
   WriteCard(HIGH(S), 0);
   WriteLn;
END Out;
BEGIN
   Out("");
END N.



Sat, 17 Oct 1992 14:34:00 GMT  
 Cardinal numbers
   >
   >Is the following program legal?  What is displayed?
   >
   >
   >MODULE N;
   >FROM InOut IMPORT WriteCard, WriteLn;
   >PROCEDURE Out(S : ARRAY OF CHAR);
   >BEGIN
   >   WriteCard(HIGH(S), 0);
   >   WriteLn;
   >END Out;
   >BEGIN
   >   Out("");
   >END N.

The original PDP11 Modula-2 compiler (which I am still using) always
places a 0C at the end of any literal string, including '""'. Thus the
above HIGH(S) value is printed out as (and is) 0. For the above, SIZE(S)
is 1.

I believe most or all other M2 compilers do the same thing, but don't
think that it is specified in PIM2, 3rd edition. Is this standard or
becoming standard?

Another related thing that I have wondered about is the situation with
respect to dynamic arrays and LONGCARD vs. CARDINAL indices. ie, how is
the TYPE of HIGH() maintained correctly when the index could be a LONG...?

The TDI compiler for the Amiga will react to the following :

          MODULE N;
          FROM InOut IMPORT WriteCard, WriteLn;
          VAR  c :CARDINAL;
               a :ARRAY[0..65536] OF CHAR;
          BEGIN
          WriteCard(HIGH(a), 0); WriteLn;
          END N;

with a diagnostic on compiling that HIGH(a) is the wrong type. It does
seem to type it as LONGCARD.

Unfortunately, the situation is not so good for the following :

          MODULE N;
          FROM InOut IMPORT WriteCard, WriteLn;
          VAR  c :CARDINAL;
               a :ARRAY[0..65536] OF CHAR;

             PROCEDURE P(s:ARRAY OF CHAR);
             BEGIN
             WriteCard(HIGH(s), 0); WriteLn;
             END;

          BEGIN
          P(a);
          END N;

This compiles without error but prints out out HIGH(s) as a CARDINAL "1"
(ie, the first word, 68000 style, of the 32 bit value).
(Their release notes do warn about this bug, however).

Do other compilers that support both CARDINAL and LONGCARD TYPES and that
allow either to be array indices handle this better?

Is this a settled issue in the standardization efforts?

            - Tom Breeden


            - Tom Breeden




Sat, 17 Oct 1992 19:06:00 GMT  
 Cardinal numbers
/
/Is the following program legal?  What is displayed?
/
/
/MODULE N;
/FROM InOut IMPORT WriteCard, WriteLn;
/PROCEDURE Out(S : ARRAY OF CHAR);
/BEGIN
/   WriteCard(HIGH(S), 0);
/   WriteLn;
/END Out;
/BEGIN
/   Out("");
/END N.

This legal program outputs the digit 0 followed by a newLine.

It is impossible to have an array whose length is zero, since that
would require its ending index to be less than its starting index.
So the type of "" is ARRAY [0..0] OF CHAR, and its length is therefore
one character.  Normally, the compiler stores 0C in such a string.

Therefore, HIGH never needs to return -1 as the maximum index of an
array.  But open array parameters which are strings should always be
checked for the presence of 0C, starting with index 0.

Marking the end of strings with a special character is an irredeemably
brain-damaged idea.  The fact that C does it is no excuse.  

--

Disclaimer: Do not confuse my views with the official views of Paradyne
            Corporation (regardless of how confusing those views may be).
Motto: Never put off to run-time what you can do at compile-time!  



Sat, 17 Oct 1992 05:54:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Cardinal numbers or unsigned int..

2. Cardinal number output

3. CARDINAL and INTEGER number in Mod-2

4. Numbers Numbers Numbers

5. (in)compatibility between INTEGER and CARDINAL

6. (Copy) (in)compatibility between INTEGER and CARDINAL

7. from ORD to CARDINAL to CONVERT

8. Typecasring SHORTCARD/CARDINAL/LONGCARD

9. INC(CARDINAL,INTEGER) problem

10. Getting a CARDINAL into a zero-padded ARRAY OF CHAR

11. Parrots and Cardinals and Ruby, oh my!

12. parrots, cardinals, and rubys - oh my!

 

 
Powered by phpBB® Forum Software