Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Data Structure problem

Posted on 2003-10-28
5
Medium Priority
?
164 Views
Last Modified: 2010-04-05
{
 
I am trying to implement a data structure like this:
 
firstNode
    |
|---|---|
c1  c2  c3
 
firstNode has 3 children...
 
Each node has a value stored... when pressing a button it would show
the values with ShowMessage.
 
THE PROBLEM: the value of firstNode (the root) is OK, but when I try
to show each children value, it shows garbage (nonsense numbers)...
 
don't know what is wrong...
 
}
 
unit uTest;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
 
  TSomethingPtr = ^TSomething;
  TSomething = class(TObject)
  protected
    value: integer;
    children: array[1..3] of TSomethingPtr;
  public
    constructor Create(num: integer); overload;
  end;
 
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    firstNode: TSomething;
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
{--------------------------------------------}
 
constructor TSomething.Create(num: integer);
begin
  inherited Create;
  value := num;
end;
 
{--------------------------------------------}
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  firstNode := TSomething.Create(0);
  firstNode.children[1] := nil;
  firstNode.children[2] := nil;
  firstNode.children[3] := nil;
end;
 
{--------------------------------------------}
 
{generates 3 children nodes, each one with a different value and add
its address to the firstNode array}
 
procedure TForm1.Button1Click(Sender: TObject);
var newNode: TSomeThing;
begin
  newNode := TSomeThing.Create(1);
  firstNode.children[1] := @newNode;
 
  newNode := TSomeThing.Create(2);
  firstNode.children[2] := @newNode;
 
  newNode := TSomeThing.Create(3);
  firstNode.children[3] := @newNode;
end;
 
{--------------------------------------------}
 
{shows the value stored in firstNode, and the values stores in each children}
 
procedure TForm1.Button2Click(Sender: TObject);
var nodePtr: TSomeThingPtr; v: integer;
begin
  ShowMessage('first node = ' + IntToStr(firstNode.value));
 
  nodePtr := firstNode.children[1];
  v := nodePtr^.value;
  ShowMessage('first child = ' + IntToStr(v));
 
  nodePtr := firstNode.children[2];
  v := nodePtr^.value;
  ShowMessage('second child = ' + IntToStr(v));
 
  nodePtr := firstNode.children[3];
  v := nodePtr^.value;
  ShowMessage('third child = ' + IntToStr(v));
end;
 
{--------------------------------------------}
 
end.
0
Comment
Question by:LeoHyppolito
[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
  • 2
  • 2
5 Comments
 

Expert Comment

by:mertero
ID: 9639779
In Button1click (when you create your node), do like this -

new(firstNode.children[1])

instead of the "Create" method.


Then you can do -

firstNode.chlidren[1]^.value = 1; // or whatever

R.

0
 

Expert Comment

by:mertero
ID: 9639781
BTW - you can get a good example on trees here -

http://www.awitness.org/delphi_pascal_tutorial/source/insert_binary_tree.html

M.
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 9639919
try this

procedure TForm1.Button1Click(Sender: TObject);
var newNode: TSomeThing;
begin
  newNode := TSomeThing.Create(1);
  firstNode.children[1] := newNode;  //no @
 
  newNode := TSomeThing.Create(2);
  firstNode.children[2] := newNode;
 
  newNode := TSomeThing.Create(3);
  firstNode.children[3] := newNode;
end;
 

procedure TForm1.Button2Click(Sender: TObject);
var nodePtr: TSomeThingPtr; v: integer;
begin
  ShowMessage('first node = ' + IntToStr(firstNode.value));
 
  nodePtr := firstNode.children[1];
  v := nodePtr.value;    //no ^
  ShowMessage('first child = ' + IntToStr(v));
 
  nodePtr := firstNode.children[2];
  v := nodePtr.value;
  ShowMessage('second child = ' + IntToStr(v));
 
  nodePtr := firstNode.children[3];
  v := nodePtr.value;
  ShowMessage('third child = ' + IntToStr(v));
end;
 
meikl ;-)

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9639922
btw.

replace this
children: array[1..3] of TSomethingPtr;
with
children: array[1..3] of TSomething;

because an object is just a pointer
-> a pointer of pointer is not needed

meikl ;-)
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9640911
meikl, your solution seems okay but perhaps there's a reason why Leo choose to use this technique. Let's see...

The TSomething contains three pointers to an object. Since an object is actually a pointer by itself, we now have a pointer to a pointer to some data. Let's look at this:

  newNode := TSomeThing.Create(1);
  firstNode.children[1] := @newNode;

Nice, but it has a flaw. firstNode.children[1] is of type TSomeThingPtr. You are not assigning an address of an object to this pointer but you're assigning the object itself to this pointer. Instead you first have to reserve memory space for this pointer. Thus:

  New(firstNode.children[1])
  newNode := TSomeThing.Create(1);
  firstNode.children[1]^ := @newNode;

Yeah, it looks funny, I know...
Next we have this:

  nodePtr := firstNode.children[1];
  v := nodePtr^.value;
  ShowMessage('first child = ' + IntToStr(v));

Well, since nodePtr is of type TSomeThingPtr it should provide the right value. IE, a pointer to the object. The object itself is at nodePtr^ so this code should work...
Why it didn't work at first? Well, nodePtr is the address of the object. nodePtr^ is pointing to some data inside the object but Delphi thinks you're actually pointing to the object and thus has to step a bit further to access the data.

But meikl, you're right. Your solution is a lot easier. I wonder why Leo did not use it. ;-)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

604 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