Solved

dynamic array of records in dynamic array of records!!

Posted on 2003-11-20
8
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

623 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