Solved

dynamic array of records in dynamic array of records!!

Posted on 2003-11-20
8
446 Views
Last Modified: 2010-04-05
Hey all,

I have a couple of records as follows :

type
 WEAPON = record
  size : byte;
  posX : double;
  posY : double;
  posZ : double;
  rotX : double;
  rotY : double;
  rotZ : double;
end;

type
 DATA = record
  LowestX : double;
  LowestY : double;
  HighestX : double;
  HighestY : double;
  weight : double;
  braking : double;
  power : double;
  hitpoints : integer;
  cost : integer;
  name : string;
  path : string;
  WeaponsNumber : byte;
  Weapons : array of WEAPON; //<---- right here!
end;

As u see, the DATA record has an array of WEAPON at the bottom.... ( as shown)

now i have a global dynamic array of DATA which works fine,

  Tanks : array of DATA;

and i can call stuff like

SetLength(Tanks, 100);
Tanks[0].weight = 1000;

but when i call SetLength on the WEAPONS record within the DATA one, an access violation occurs...
i.e.
SetLength(Tanks[0].Weapons, 100); //Access violation!!!

Please help!!!

Regards,
-Rob-
0
Comment
Question by:Excalibur81
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Accepted Solution

by:
jconde earned 50 total points
ID: 9795422
I tested the following code out and worked fine:

procedure TForm1.Button1Click(Sender: TObject);
var
Tanks : array of DATA;
begin
  SetLength(Tanks, 100);
  Tanks[0].weight := 1000;
  SetLength(Tanks[0].Weapons, 100);
  tanks[0].Weapons[0].size := 9;
  showmessage(inttostr(tanks[0].Weapons[0].size));
  tanks[0].Weapons[2].size := 54;
  showmessage(inttostr(tanks[0].Weapons[2].size));
end;

WinXP Pro / D7 Ent
0
 
LVL 7

Expert Comment

by:jconde
ID: 9795430
BTW, even though it would slightly add complexity to your code, I would suggest you use a TList for this instead of a dynamic array.
0
 
LVL 17

Expert Comment

by:geobul
ID: 9795449
Hi,

No access violation occurs in my Delphi 5. Everything is just fine:

var
  Form1: TForm1;
  Tanks : array of DATA;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetLength(Tanks, 100);
  Tanks[0].weight := 1000;
  SetLength(Tanks[0].Weapons, 100);
  Tanks[0].Weapons[0].PosX := 100.25;
  ShowMessage(FloatToStr(Tanks[0].Weapons[0].PosX));
end;

Regards, Geo
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 9796061
btw, you could use length(tanks[n].weapons) instead of tanks[n].weaponnumber ;)
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Excalibur81
ID: 9796252
Ahhh i see now, it was a logic mistake :)

Hmm, so since there was actually no problem, how do points work??


Also.....

"BTW, even though it would slightly add complexity to your code, I would suggest you use a TList for this instead of a dynamic array. "
Unfortunatly, im saving the data in a file which will be read later on by a program in C++, and needs to be in this fairly specific format.... thanks anyways :)

"btw, you could use length(tanks[n].weapons) instead of tanks[n].weaponnumber ;) "
Yes i had seen that, but for now im just using this for debugging purposes, but thanks also :)

0
 
LVL 7

Expert Comment

by:jconde
ID: 9800229
I beleive you should split the points between geobul and myself.  geobul, what do you suggest ?
0
 
LVL 17

Expert Comment

by:geobul
ID: 9802595
jconde should get the points. I just confirmed that for D5.
0
 

Author Comment

by:Excalibur81
ID: 9805327
Thanks guys :)

Soz, i meant to split the points, but ive never done before, and i thought it was inside the "Accept Answer" path....

but i guess geobul didnt want them anyways :D


Thanks!!!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now