[fpc-pascal] Variants vs. Variant Records

David Emerson dle3ab at angelbase.com
Sun Jul 24 04:04:28 CEST 2005

keep in mind that when Pascal stores a variant record, each record is the same size -- the maximally sized variant.

So, for example, if you have

type my_variant_record = record
case rec_type : longint of
   1: ( my_bool : boolean );
   2: ( p_to_ss  : ^shortstring );
   3: ( x, y : longint );

Then All records of this type will be the maximum size, which would be three longints: rec_type, x, y (for the case 3). So even when you only have case 1, a boolean and the first longint, that record size would be the maximum size 3 * sizeof (longint) = 3 * (4 bytes) = 12 bytes.

Then, you could also do something like

   primitive_value_type = (prim_value_1_shortstring, prim_value_2_real, etc...);
   my_variant_array_element = record
      elem_type : primitive_value_type;
      p : pointer;   // points to a variable of type elem_type;

I don't know if the above will work. It may be that you would have to structure it as a variant record:

   my_variant_array_element = record
      case elem_type : primitive_value_type of
         prim_value_1_shortstring : ( shortstring_pointer : ^shortstring );
         prim_value_2_real : (real_pointer : ^real );

(Note: my syntax for those pointers might not be correct; I don't use pointers much.)

Last I checked (fpc 1.9.4) it was not possible to give variant array elements of the same size and position duplicate names, so even though they're all pointers, you could not give the same name, e.g. "p" to "shortstring_pointer" and "real_pointer". This should not be too big a deal, because you really ought to know what you're working with all the time anyway. But there was one time when I had a record type that was somewhat similar to three variants like (x, a) (x, b) (q, r, s, t) and I just had to bring x out of the variant types even though it would have been sensible in this case to have x address the first longint in variants type 1 and 2. Oh well.

The nice thing about the latter two methods is that you get an array with relatively small elements, so if your arrays are sizeable, it should save memory. If the arrays are not too big, the first one might be the way to go, since it ought to be slightly faster (saving only dereferencing a pointer in some variant cases)

I guess I don't actually know what you mean by "variant arrays"!


 -------- Original Message --------
> From: Agustin Barto <abarto at gmail.com>
> Sent: Saturday, July 23, 2005 5:57 PM
> To: FPC-Pascal users discussions <fpc-pascal at lists.freepascal.org>
> Subject: [fpc-pascal] Variants vs. Variant Records
> I have to build some container classes (like java Vector) for a
> project. I was thinking on how to store some primitive values (so far
> I only need to store Integer, Real or Extended and ShortString) and I
> can't decide between variants (variant arrays) and variant records
> (dynamic arrays of variant records). Any hints?
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal 

More information about the fpc-pascal mailing list