Solved

Data Structure problem

Posted on 2003-10-28
5
158 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
  • 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 50 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing problem 2 95
Dev Express grid collapse 2 45
scroll down TListBox component in Delphi 1 11
Delphi Seattle StructureView color 1 5
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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