Board index » delphi » Dynamic array in a record

Dynamic array in a record

Hi everyone,

I have a record that consists of differnt types. I wanted to add a
dynamic array in the record. I am using a TList for the main record
(playerdata):
playerlist:tlist;
p:^playerdata; (and then I can add to p the different records).

The problem is when I tried to add the dynamic array and then
setlength(p.actual_s,10) I get an Access violation error.

Any kind person wants to give me a hand? It is driving me crazy

Thank you!

skills = record
    case boolean of
        true:(sc,op,bc,pa,ae,co,ta,dp,ha:integer;);
        false:(skillset:array[1..9]of integer;)
  end;

playerdata = record
     name : string[40];
     starter: string[2];
     actual_s : array of skills   <---- Dynamic array of records
end

 

Re:Dynamic array in a record


if  your record is dynamic then you must also pass the same info to the
setlength.
SetLenght(P^.actual_s,10);
this assumes that you also used New/Getmem when creating the Pointer to
store in the Tlist for each record created ?
Quote
Jake wrote:
> Hi everyone,

> I have a record that consists of differnt types. I wanted to add a
> dynamic array in the record. I am using a TList for the main record
> (playerdata):
> playerlist:tlist;
> p:^playerdata; (and then I can add to p the different records).

> The problem is when I tried to add the dynamic array and then
> setlength(p.actual_s,10) I get an Access violation error.

> Any kind person wants to give me a hand? It is driving me crazy

> Thank you!

> skills = record
>     case boolean of
>         true:(sc,op,bc,pa,ae,co,ta,dp,ha:integer;);
>         false:(skillset:array[1..9]of integer;)
>   end;

> playerdata = record
>      name : string[40];
>      starter: string[2];
>      actual_s : array of skills   <---- Dynamic array of records
> end

Re:Dynamic array in a record


P.S.
  you should also be NILLing the array when you create the record before

using SetLength other wise the RTTL may get a little confused on wether
its
already has been initiated.

Quote
Jake wrote:
> Hi everyone,

> I have a record that consists of differnt types. I wanted to add a
> dynamic array in the record. I am using a TList for the main record
> (playerdata):
> playerlist:tlist;
> p:^playerdata; (and then I can add to p the different records).

> The problem is when I tried to add the dynamic array and then
> setlength(p.actual_s,10) I get an Access violation error.

> Any kind person wants to give me a hand? It is driving me crazy

> Thank you!

> skills = record
>     case boolean of
>         true:(sc,op,bc,pa,ae,co,ta,dp,ha:integer;);
>         false:(skillset:array[1..9]of integer;)
>   end;

> playerdata = record
>      name : string[40];
>      starter: string[2];
>      actual_s : array of skills   <---- Dynamic array of records
> end

Other Threads