Board index » delphi » Problems with array in record

Problems with array in record

Does anybody knows is there problems with allocating memory, if you have
array in record.

PTRecord = ^TRecord;
TRecord = record
  ...
  DataArray: array(1..ConsLength) of String;
  ...
end;

var
  CurRecord: PTrecord;

...
getmem(CurRecord, sizeof(TRecord));

CurRecord^.DataArray(1) := X;    here a get an error.

....

Somebody knows why?

Thank you,
Marjan Jesenovec

 

Re:Problems with array in record


On Thu, 27 Nov 1997 11:11:57 +0100, Marjan Jesenovec <h...@eunet.si>
wrote:

Quote
>Does anybody knows is there problems with allocating memory, if you have
>array in record.

>PTRecord = ^TRecord;
>TRecord = record
>  ...
>  DataArray: array(1..ConsLength) of String;
>  ...
>end;

>var
>  CurRecord: PTrecord;

>...
>getmem(CurRecord, sizeof(TRecord));

>CurRecord^.DataArray(1) := X;    here a get an error.

I'm not sure if you copied and pasted your source or if you
wrote from memory. Anyway, you will have to use brackets, and not
parantheses as above.

Like
 DataArray: Array[1..ConsLength] of String;

 CurRecord^.DataArray[1] := X; // this should work I reckon

-

Cheers,
Svein

        __    __    __    __
       /\ \  /\ \  /\ \  /\_\  Real programmers think
      /  \_\/  \ \/  \ \/ / /_  structured programming
     / /\ | |/\ \_\/\ \_\/_/\_\  is a communist plot.
    / /_/ |_| / / /\/ / /\/ / /
   /  \/ / /_/ / /\  / /\  / / Name: Svein E Kvernoey
  /  \__/_/\/ / /_/ / /_/ / /  Nick: Boah@IRC
 / /\ | | \  / / / / / / / /   mail: sve_k...@hig.no
/ /_/ |_|  \/_/  \/_/  \/_/    mail: b...@giraff.hig.no
\ \/ / /
 \__/_/      CM2 Page: http://www.hig.no/~sve_kver/cm2/

Re:Problems with array in record


Quote
On Thu, 27 Nov 1997 15:48:15 GMT, b...@giraff.hig.no (Boah) wrote:

>I'm not sure if you copied and pasted your source or if you
>wrote from memory. Anyway, you will have to use brackets, and not
>parantheses as above.

>Like
> DataArray: Array[1..ConsLength] of String;

> CurRecord^.DataArray[1] := X; // this should work I reckon

Humm...

 Come to think of it, this is wrong too, unless X is a variable of
a Char type.

 CurRecord^.DataArray[1] := 'X'; // sets first letter to 'X'

This should be correct. Sorry to follow up my own posting, but
I'd better  correct my own error before someone else does. :)
Unless I have blown it once again.

have a nice day,
Svein

        __    __    __    __
       /\ \  /\ \  /\ \  /\_\  Real programmers think
      /  \_\/  \ \/  \ \/ / /_  structured programming
     / /\ | |/\ \_\/\ \_\/_/\_\  is a communist plot.
    / /_/ |_| / / /\/ / /\/ / /
   /  \/ / /_/ / /\  / /\  / / Name: Svein E Kvernoey
  /  \__/_/\/ / /_/ / /_/ / /  Nick: Boah@IRC
 / /\ | | \  / / / / / / / /   mail: sve_k...@hig.no
/ /_/ |_|  \/_/  \/_/  \/_/    mail: b...@giraff.hig.no
\ \/ / /
 \__/_/      CM2 Page: http://www.hig.no/~sve_kver/cm2/

Re:Problems with array in record


Quote
> Does anybody knows is there problems with allocating memory, if you have
> array in record.

> PTRecord = ^TRecord;
> TRecord = record
>   ...
>   DataArray: array(1..ConsLength) of String;
>   ...
> end;

> var
>   CurRecord: PTrecord;
>> ...
> getmem(CurRecord, sizeof(TRecord));

> CurRecord^.DataArray(1) := X;    here a get an error.

You don't say which version of Delphi you are using, I stand to be
corrected but definitely in Delphi 3 (I think also 2) the string variable
is compatible to PChar. In other words it is a pointer. To use the old
style fixed length strings you have to explicitely declare them.
In other words your definition is an array of unassigned pointers as
opposed to fixed strings. Try

 PTRecord = ^TRecord;
 TRecord = record
   ...
   DataArray: array(1..ConsLength) of String[50];   {or whatever size you
want}
   ...
 end;

If you want to stay compatible to the string/pchar convention the use
something like

 PTRecord = ^TRecord;
 PseudoStr = array [0..50] of char;

 TRecord = record
   ...
   DataArray: array(1..ConsLength) of PseudoString;
   ...
 end;

Regards
Mike Heydon

Other Threads