[M3devel] heap vs. stack? (sort of)

Henning Thielemann lemming at henning-thielemann.de
Mon Jan 28 12:39:10 CET 2008


On Mon, 28 Jan 2008, Jay wrote:

> Is it possible to have this pattern in Modula-3:
>
> PROCEDURE Parse (nm: TEXT;  host: BOOLEAN): T =  VAR len := Text.Length (nm);   buf: ARRAY [0..255] OF CHAR;  BEGIN    IF (len <= NUMBER (buf))      THEN RETURN DoParse (nm, SUBARRAY (buf, 0, len), host);      ELSE RETURN DoParse (nm, NEW (REF ARRAY OF CHAR, len)^, host);    END;  END Parse;
>
> PROCEDURE DoParse (nm_txt: TEXT;  VAR nm: ARRAY OF CHAR;  host: BOOLEAN): T =

I think it is a good idea to encapsulate the common code in DoParse. If
DoParse needs local variables, it can be turned into a local PROCEDURE of
Parse. On the other hand I doubt that it is sensible to reserve 256 bytes
on the stack also if they are not used, or only a part of them are used.
What's bad about always using NEW? I think Modula-3's memory management
has optimizations for small amounts of memory.



More information about the M3devel mailing list