Autor Beitrag
delphisual
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 89

WIN 2000
D5 Prof
BeitragVerfasst: Di 22.07.03 21:46 
Hallo zusammen,

Ein neuer Tag eine neue Frage ;), und zwar: ich möchte für jedes einzelnes Element meines Arrays einen Pointer bereitstellen um eventuell den Speicherplatz zusparen.

Es funktioniert soweit, ich habe aber trotzdem das Gefühl, dass die einzelne Elemente nicht die Speicheradresse des Pointers bekommen, sondern nur einen Wert des Pointer.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
    TAdress = record
    Name:string[10];
    end;
  TForm1 = class(TForm)
    Edit1: TEdit;
    Label1: TLabel;
    Button1: TButton;
    Button2: TButton;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
   Adress:Array of TAdress; // 1D dynamisches Array
   i: integer;
   Pvar: ^string;           // Mein Pointer
   Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
     New(pvar);               // Meinem Pointer Speicher reservieren
     pvar^:=Form1.Edit1.Text; // Meinem Pointer die Inhalt zuweisen
     i:=i+1;
     SetLength(Adress,i+1);   // Länge des Arrays beim jeden click erhöhen
     Adress[i].Name:=pvar^;   // Dem aktuelen Element den Wert des Pointers übergeben
     Form1.Label1.Caption:=Adress[i].Name; // Aktuellen Element anzeigen
     Dispose(pvar);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Adress:=NIL;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Form1.Label2.Caption:=Adress[9].Name;      // Den 10en Element anzeigen
end;

procedure TForm1.FormCreate(Sender: TObject);
begin

i:=-1;
end;

end.

Ich denke der richtige Weg wäre es, bereits im Record mit Pointer zuarbeiten. Bitte gibt den richtigen Lösungsvorschlag Problem.

Ich danke für Alle Antworten

_________________
(Wer ein Problem definiert, hat es schon halb gelöst.)
specialwork
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 52

Windows XP Professional; Windows Server 2003
Delphi 7 Prof, Delphi 8.Net
BeitragVerfasst: Mi 23.07.03 01:04 
Titel: TList als Alternative
Hallo delphisual,

Warum schwierig , wenn's auch einfach geht !

Benutze doch einfach die Klasse TList. Die ist geradezu predisziniert für Deinen Fall.

Ich habe Dir mal ein kleines Demoprogramm erstellt. Dabei handelt es sich um eine Adressverwaltung, die nur Adressen im flüchtigen Speicher aufnehmen und halten kann.

Gruß, Tom

Source:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
unit Unit1; 

interface 

uses 
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
  StdCtrls, Spin;

type 
  TAdress = record
    Vorname: String;
    Nachname: String;
    Postleitzahl: String;
    Strasse: String;
    Ort: String;
  end;

  TForm1 = class(TForm)
    Edit1: TEdit;
    Button2: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    SpinEdit1: TSpinEdit;
    Label6: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
    procedure SpinEdit1Change(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    AdressList: TList;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  AdressList := TList.Create;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  AdressList.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  pNewAdress: ^TAdress;
begin
  New(pNewAdress);

  pNewAdress.Vorname      := Edit1.Text;
  pNewAdress.Nachname     := Edit2.Text;
  pNewAdress.Strasse      := Edit3.Text;
  pNewAdress.Postleitzahl := Edit4.Text;
  pNewAdress.Ort          := Edit5.Text;

  AdressList.Add(pNewAdress);

  SpinEdit1.MaxValue      := AdressList.Count;
  SpinEdit1.Value         := SpinEdit1.MaxValue;
end;

procedure TForm1.SpinEdit1Change(Sender: TObject);
var
  AdressCursor: ^TAdress;
begin
  if (SpinEdit1.Value >= 1and (SpinEdit1.Value <= AdressList.Count) then begin
    AdressCursor    := AdressList.Items[SpinEdit1.Value-1];

    Edit1.Text      := AdressCursor.Vorname;
    Edit2.Text      := AdressCursor.Nachname;
    Edit3.Text      := AdressCursor.Strasse;
    Edit4.Text      := AdressCursor.Postleitzahl;
    Edit5.Text      := AdressCursor.Ort;
  end else begin
    SpinEdit1.Value := 0;
    
    Edit1.Text      := '';
    Edit2.Text      := '';
    Edit3.Text      := '';
    Edit4.Text      := '';
    Edit5.Text      := '';
  end;
end;

end.


Zuletzt bearbeitet von specialwork am Do 24.07.03 00:36, insgesamt 1-mal bearbeitet
delphisual Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 89

WIN 2000
D5 Prof
BeitragVerfasst: Mi 23.07.03 16:26 
Hallo bspecialwork,

danke für deine Antwort!
Dein Beispiel werde ich auf jeden Fall näher anschauen! :shock:
Ich bin dankbar für jede Hilfestellung :!:

MfG delphisual :?

_________________
(Wer ein Problem definiert, hat es schon halb gelöst.)
Taurin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 34

Win XP
D6 Personal
BeitragVerfasst: Mi 23.07.03 18:03 
Möglicherweise hast du Programm ja simplifiziert, um den Blick auf den Kern der Sache zu lenken, ich versteh aber nicht ganz, warum du hier rumpointerst....
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.Button1Click(Sender: TObject); 
begin  
     inc(i);
     SetLength(Adress,i+1);        
     Adress[i].Name          := Form1.Edit1.Text;
     Form1.Label1.Caption := Form1.Edit1.Text; 
end;

So ist es doch gleich viel hübscher :D

Zudem ist TList auch kein schlechter Vorschlag.
delphisual Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 89

WIN 2000
D5 Prof
BeitragVerfasst: Mi 23.07.03 18:24 
Hi,
ich möchte dass jedes Element meines Array nur einen Pointer erhält was bei der Speichergrösse bei grösseren Arrays sehr hilfreich sein sollte!

kennt sich wer da aus???

THX

_________________
(Wer ein Problem definiert, hat es schon halb gelöst.)