Organization: FidoNet node 1:139/590 - PMI BBS, Green Bay WI
Lines: 105
MO> Here`s a question for networked JPI-supporters:
MO> We tried to `overload` the NEW-procedure by implementing the module
MO> DEFINITION MODULE XStorage;
MO> FROM SYSTEM IMPORT ADDRESS;
MO> BEGIN
MO> ...
MO> PROCEDURE NEW ( VAR adr : ADDRESS );
MO> BEGIN
MO> ALLOCATE( adr, SIZE(adr^) );
MO> END NEW.
MO> END XStorage.
MO> Our intention was to avoid substituting NEW-calls in a number of source-MO> files by ALLOCATE() calls. This doesn`t work, because the compiler MO> allocates 4 bytes of memory, although the amount of x bytes, required MO> for the data structure adr points at, should be allocated, eg.
MO> TYPE
MO> RecordPtr = POINTER TO Record;
MO> Record = RECORD
MO> str : ARRAY [0..79] OF CHAR;
MO> int : INTEGER;
MO> END;
MO> XStorage.NEW( adr ) allocates 4 bytes of memory. An explicit MO> ALLOCATE()-call works.
MO> Got the point?
MO> Marcus
This isn't a compiler bug, overloading NEW() cannot be done in this way. When the compiler encounters a call to NEW() it will normally expand this to a call of the form:
ALLOCATE(adr, SIZE(adr^));
as you have determined. In order for this to work, the compiler must have information about the type of the variable in the NEW() call. This size information is not passed to your overloaded NEW().
The solution is to overload not the NEW() procedure but the ALLOCATE() and DEALLOCATE() procedures. Since these are not compiler generated you can be sure of getting all the relevant information, you can then do whatever you like in your own code. e.g: