[comp.lang.pascal] Help: How can I implement a record with variable width

mpe@shamash.cdc.com (Mike Ebsen) (01/07/91)

I would like to implement a variable record length record using Turbo
pascal 5.5.

type
  element_type = record
    number_of_items : word;
    items : array [1..number_of_items] of real;
  end;

I've seen variant record structures before, but nothing which allows the 
size of an array of modulate as a function of a variable contained within
the record.

Any possible clues or suggestions are welcomed.
   
   mpe@shamash.cdc.com

dslg0849@uxa.cso.uiuc.edu (Daniel S. Lewart) (01/07/91)

mpe@shamash.cdc.com (Mike Ebsen) writes:

> I would like to implement a variable record length record using Turbo
> pascal 5.5.
>
> type
>   element_type = record
>     number_of_items : word;
>     items : array [1..number_of_items] of real;
>   end;
>
> I've seen variant record structures before, but nothing which allows the
> size of an array to modulate as a function of a variable contained within
> the record.
-------------------------------------------------------------------------------
type
  TRealArray = array[1..10000] of Real;
  element_type = record
    number_of_items : word;
    ItemsPtr : ^TRealArray;
  end;

var
  Element: element_type;
Begin
  { ... Set Element.number_of_items ... }
  with Element do
    GetMem(  ItemsPtr, SizeOf(Real)*number_of_items );
  { ... }
  with Element do
    FreeMem( ItemsPtr, SizeOf(Real)*number_of_items );
  { ... }
End.
-------------------------------------------------------------------------------
Your problem calls for dynamic arrays, not variant records.  The above code
does what you want.

Daniel Lewart
d-lewart@uiuc.edu

zhou@brazil.psych.purdue.edu (Albert Zhou) (01/07/91)

Pascal does not support variable length arrays.

eli@smectos.gang.umass.edu (Eli Brandt) (01/10/91)

In article <11660@j.cc.purdue.edu> zhou@brazil.psych.purdue.edu (Albert Zhou) writes:
>Pascal does not support variable length arrays.

Actually, Pascal does, though TP doesn't implement it.  There are two "levels" of 
Standard Pascal: 0 and 1.  Level 1's only difference is that it supports variable-
length arrays and is almost never implemented.

As an unrecontructed C user, I do the following:

type
  VarArrType = array[1..1] of integer;

function UseVarArray(elements: integer; varr: VarArrType);
var i: integer;
begin
  for i := 1 to elements do writeln(varr[i]);
end;

with {$R-}, of course, and if you trash the OS doing this, well, be more careful.

*Why* couldn't Borland make $PUSH and $POP compiler-flag-state directives?
As it is, you when you want to make your final binary you have to go through the
code killing the {$R-} code... {$R+} pairs.  Or have your editor run your code 
through cpp before compiling, which does have its attendant benefits.

reino@cs.eur.nl (Reino de Boer) (01/10/91)

eli@smectos.gang.umass.edu (Eli Brandt) writes:

>As an unrecontructed C user, I do the following:

>type
>  VarArrType = array[1..1] of integer;

>function UseVarArray(elements: integer; varr: VarArrType);
>var i: integer;
>begin
>  for i := 1 to elements do writeln(varr[i]);
>end;

or:
const	arl	=  5;
	aru	= 10;
	arsize	= aru - arl + 1;
var	ar	: array [arl..aru] of integer;
procedure WritelnIntArray( elements : integer; var intarray );
	var	a : array [1..maxint] of integer absolute intarray;
		i : integer;
	begin
		for i := 1 to elements do
			writeln( a[i] )
	end;
begin
	WritelnIntArray( arsize, ar )
end.
-- 
Reino R. A. de Boer     "We want to build the right product right, right?"
Erasmus University Rotterdam ( Informatica )
e-mail: reino@cs.eur.nl