🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #3774

Re: Pointers and whatnot

#3774
From: Duncan C White
Date: Mon, 24 Aug 1987 22:23
140 lines
5389 bytes
In article <748@vixie.UUCP> paul@vixie.UUCP (Paul Vixie Esq) writes:
>In article <8708220025.AA10833@cayuga.cs.rochester.edu>...
>BOTCHAIR@UOGUELPH.BITNET (Alex Bewley) writes:
>#TYPE PtrType = POINTER TO DataType;
>#     DataType = RECORD
>#                 Text : ARRAY [0..80-1] OF CHAR;
>#                 next : PtrType;
>#               END;
>#
>#VAR  Ptr : PtrType;
>#
>#BEGIN
>#  NEW(Ptr); (* or ALLOCATE(Ptr, TSIZE(DataType); *)
>#  Assign(Ptr^.Text,"This is some text...");
...
>#   In that above case, Text is 80 chars, but suppose the length of Text
>#varies. How would I be able to change the type?
>
>You could use something like
>
>TYPE DataType = RECORD
>	Text : POINTER TO CHAR;
>	next : PtrType;
>     END;
>
>BEGIN
>  NEW(Ptr);  ALLOCATE(Ptr.Text, strlength("what you want to put there"));
>  Assign(Ptr^.Text^,...
>

I'm afraid this is incorrect, Paul: in Modula-2, POINTER TO CHAR is precisely
what it says: a pointer to a SINGLE character.  Not a pointer to an array of
characters, or anthing else...

C, on the other hand, makes no distinction between POINTER TO CHAR,
ARRAY OF CHAR, and [to an extent] POINTER TO ARRAY OF CHAR..

Maybe the confusion arises due to the fact that the ALLOCATE procedure,
being nothing but a library procedure, will happily let you:

	ALLOCATE( Ptr^.Text, 10 );

[ Despite the fact that the size of Ptr^.Text is one byte... not 10 words ]

However, doing such an operation makes little sense, since you can only
use

	Ptr^.Text^

which is the first character of that block of 10 characters... the remaining
nine characters can only be accessed by exceedingly messy type conversions,
which involve type casting the pointer into a pointer to some other type [an
array or record] and then accessing the fields or elements through the
fiddled pointer...

One possible answer to Alex's problem could be:

TYPE
	HugeStrPtr = POINTER TO ARRAY [ 0..MaxInt ] OF CHAR;

	DataType = RECORD
		Text : HugeStrPtr;
		next : PtrType;
	END;

VAR
	Ptr : POINTER TO DataType;

BEGIN
	NEW(Ptr);
	HugeAssign( Ptr^.Text, "what you want to put there" );
...

Unfortunately, you then have to define HugeAssign yourself: perhaps as:

	PROCEDURE HugeAssign( VAR H : HugeStrPtr; S : ARRAY OF CHAR );

	VAR length, posn : INTEGER;

	BEGIN
		length := strlength( S );
		ALLOCATE( H, length );
		FOR posn := 1 TO length DO
			H^[posn] := S[posn-1];
		END;
		H^[ 0 ] := CHR( length );
		(* if you want strings longer than 256, two characters
		 * would have to be used for the length count...
		 *)
	END HugeAssign;

Offhand, I can't think of a better way to represent strings if you really
care about saving space [otherwise, I guess you just make an artibrary upper
limit : eg. define String = ARRAY[ 1..200 ] OF CHAR and ignore the wastage..]


Paul Vixie continues [quoting Alex:]
>#   Or suppose I just want 137 bytes of storage of no particular type, can one
>#have just a POINTER with no TO?  According to my compiler, no (Logitech
>#Modula-2/86).
>
>Wirth recommends POINTER TO WORD for such things; this is supposed to be
>type-compatible with all pointers.

Well, Alex, I guess it depends whether you really want 137 bytes of storage -
a byte is a well-understood concept, so 137 bytes of storage is just:

	ARRAY [ 1..137] OF SYSTEM.BYTE

or whether you truly want a 'blob' of memory of no particular type, in which
case you do:

	VAR X, Y : ADDRESS;	(* same as POINTER TO WORD *)

	BEGIN
		ALLOCATE( X, 137 );	(* note: 137 words, not bytes *)

If you do this, however, you must use expressions like:

	Y := X + 50;
	... Y^ ...

to access the 50th byte [or word on some systems] of X.

>-- 
>Paul A. Vixie, Esq.                  "A viler evil than to throw a man into a
>paul%vixie@uunet.uu.net              sacrificial furnace, is to demand that he
>{uunet,ptsfa,hoptoad}!vixie!paul     leap in, of his own free will, and that he
>San Francisco, (415) 647-7023	      build the furnace, besides."  (Ayn Rand)

-----------------------------------------------------------------------------
JANET address : dcw@uk.ac.ic.doc| Snail Mail :  Duncan White,
--------------------------------|               Dept of Computing,
  This space intentionally      |               Imperial College,
  left blank......              |               180 Queen's Gate,
  (paradoxical excerpt from     |               South Kensington,
  IBM manuals)                  |               London SW7
----------------------------------------------------------------------------
Tel: UK 01-589-5111 x 4982/4991
----------------------------------------------------------------------------
Technical Details
Message-ID: <520@ivax.doc.ic.ac.uk>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!seismo!mcvax!ukc!eagle!icdoc!dcw
References: <8708220025.AA10833@cayuga.cs.rochester.edu> <748@vixie.UUCP>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!seismo!mcvax!ukc!eagle!icdoc!dcw
From: dcw@doc.ic.ac.uk (Duncan C White)
Newsgroups: comp.lang.modula2
Subject: Re: Pointers and whatnot
Message-ID: <520@ivax.doc.ic.ac.uk>
Date: Mon, 24-Aug-87 18:23:38 EDT
Article-I.D.: ivax.520
Posted: Mon Aug 24 18:23:38 1987
Date-Received: Wed, 26-Aug-87 04:40:16 EDT
References: <8708220025.AA10833@cayuga.cs.rochester.edu> <748@vixie.UUCP>
Reply-To: dcw@doc.ic.ac.uk (Duncan C White)
Organization: Dept. of Computing, Imperial College, London, UK.
Lines: 140
Summary: POINTER TO CHAR not POINTER TO ARRAY OF CHAR in M2