Hi
David James wrote:
> Subject: Bug? Variants don't overlay when they contain strings
> Date: Tuesday, December 28, 1999 11:25 AM
>
> Should the different variants in a variant record overlay in the same
> memory?
>
> Previous Pascals I have used have guaranteed this, and I've got
> low-level code that relies on this.
>
> The simple example below shows that this is not true for GPC - the
version
> as reported by gpc -v is:
> Reading specs from /usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/specs
> gpc version 19991030, based on 2.95.2 19991024 (release)
>
> The output from the program is:
>
> addr of i is: -1073742896
> addr of s is: -1073742760
> addr of ic is: -1073742668
>
> program daj15(input,output);
> type trec = record
> case integer of
> 1:(i:array [1..34] of integer);
> 2:(s:string(80));
> 3:(ic:packed array[0..79] of char);
> end;
>
> var r:trec;
>
> procedure sub(r:trec);
> begin
> writeln('addr of i is: ',integer(addr(r.i)):1);
> writeln('addr of s is: ',integer(addr(r.s)):1);
> writeln('addr of ic is: ',integer(addr(r.ic)):1);
> end;
as an aside, if you change the cast "integer" to "cardinal" the output
will be a positive number
>
> begin
> sub(r);
> end.
>
> If the string s is removed from the record, then the same address is
> reported for i and ic.
>
You have more than an overlay problem. In standard pascal you expect ic[ 1
] to align
with the first character position of s and thus one character to the left
is the length of s.
Thus ic[0] is the length of s.
In extended pascal there is 8 bytes to the left of the first charactor
position makingt ic[ 0 ] useless
So you end up leaving s out of Trec but what you can do is index s like an
array[ 1..CAPACITY]
and use Setlength when you need to.
Two different kinds of strings with the same name - string - does make a
bit of confusion. Perhaps the
oldstyle strings could be renamed "short string" ?? ( didn't think
changing the spelling to "stwing"
would fly )
Russ
<russwhit(a)mind.net>