Autor Beitrag
obbba
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18

Win XP
Delphi 5, C mit Dev-C++, PureBasic
BeitragVerfasst: Fr 09.02.07 21:13 
Bei folgendem code bekomme ich einen Fehler durch "Zugriffsverletzung", wenn ich auf den Button3 klicke.
Ich verstehe das nicht. Die 3. Prozedur funktioniert doch ähnlich wie die 2. .

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:
unit Unit1;

interface

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

type
  pint= ^Integer;

  tc = class
  public
    p: pint;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private-Deklarationen }
    p,p2: Pint;
    i: integer;
    op: tc;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  i := 333;
  p := @i;
  caption := inttostr(p^);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  new(p2);
  p2^:=4;
  caption:=inttostr(p2^);
  dispose(p2);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  new(op.p);
  (op.p)^:= 1234;
  caption := inttostr((op.p)^);
  dispose(op.p);
end;

end.
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Fr 09.02.07 21:14 
Du musst imho von op erst eine Objektinstanz erstellen :!:

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
obbba Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18

Win XP
Delphi 5, C mit Dev-C++, PureBasic
BeitragVerfasst: Fr 09.02.07 21:18 
user profile iconMarco D. hat folgendes geschrieben:
Du musst imho von op erst eine Objektinstanz erstellen :!:


Falls du meinst was ich denke, ist das in Zeile 28 geschehen.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 09.02.07 21:22 
In Zeile 28 hast Du ein Feld vom Typ Tc deklariert, aber Du must noch eine Instanz anlegen:
ausblenden Delphi-Quelltext
1:
op := Tc.Create;					

Hier gibt's auch was zu Klassen & Co.


Wozu brauchst Du dieses Pointer-Gedöns eigentlich?

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
obbba Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18

Win XP
Delphi 5, C mit Dev-C++, PureBasic
BeitragVerfasst: Fr 09.02.07 21:44 
So aus Prinzip halt.
Ursprünglich wollte ich eine Klasse für Listen (Linked List) machen.
Aber vieleicht gibts da von Delphi auch schon was vorgefertigtes?

Noch was:
Solche Fehlermeldungen kommen nicht allein, sondern in Gruppen, wenn ich versuche das fehlerhafte Programm zu beenden und es neu zu kompilieren.
Meistens muss ich auch Delphi neustarten.
Geht das auch einfacher?
obbba Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18

Win XP
Delphi 5, C mit Dev-C++, PureBasic
BeitragVerfasst: Fr 09.02.07 23:39 
Soll ich am Ende destroy auch aufrufen?
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 09.02.07 23:43 
Wenn Du die Instanz nicht mehr brauchst, solltest Du Free aufrufen. Im Endeffekt ruft das auch Destroy auf, prüft aber vorher noch, ob die Instanz noch existiert. Du bekommst also eine Abfrage kostenlos dazu, ohne eine Zeile mehr zu zahlen :D

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".