Autor Beitrag
Green
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: Do 18.05.06 22:10 
habe folgenden code bei dem ich in der gehicghlighteten zeile eine access vilation krieg...

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:
function TForm1.PregMatch (const AInputString,RegEx : string) : TStringDynArray;
var
  reg: TPerlRegEx;
  i: integer;
begin
  reg         := TPerlRegEx.Create(nil);
  reg.Subject := AInputString;
  reg.RegEx   := RegEx; // Der Ausdruck
  SetLength(Result,0);
  if reg.Match() then
  begin
    repeat
      //ShowMessage(reg.SubExpressions[1]);  //  0 = gesamter Match, 1 bis n = Teilmatches
      for i:=0 to reg.SubExpressionCount do
      Result[i] := reg.SubExpressions [i];
    until not reg.MatchAgain();
    Result[10]:='';
  end else begin
  Result[10]:='false';
  end;
  reg.Free();
end;

function TForm1.ParseMessage(const AParseData : string) : TStringDynArray;
  var matches,host : TStringDynArray;
begin
  SetLength(matches,0);
  SetLength(host,0);
  SetLength(Result,6);
    matches := PregMatch(AParseData , '#^(?::(\S+)\s*)?(\w+|\d{3})((?:\s+[^:\s]\S*)*)(?:\s+\:(.*))?\s*$#');
    if matches[10]='' then begin
      host := PregMatch(matches[1] , '#^(.*)!(.*)@(.*)#');
      if host[10]='' then begin
        Result[0] := host [1];
        Result[1] := host [2];[/u]
        Result[2] := host [3];
        Result[3] := '';
      end else begin
        Result[0]  := '';
        Result[1]  := '';
        Result[2]  := '';
        Result[3]  := matches[1];
      end;
    Result[4] := matches[2];
    Result[5] := matches[3];
    //SplitRegExpr('#\s+#' , Trim(matches[3]) , Result[5]);
    Result[6] := matches[4];
  end;
end;


kann mir jemand erklären warum? hab das array doch mit SetLength(matches,0) auf unendlich bestimmt, wo is das problem?

mfG Green

€: der highlichter spinnt... das highlight sollte die ganze zeile betreffen...
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Do 18.05.06 22:22 
user profile iconGreen hat folgendes geschrieben:
hab das array doch mit SetLength(matches,0) auf unendlich bestimmt, wo is das problem?

Soll das ein Scherz sein? Seit wann ist 0 = INFINIT

_________________
Ist Zeit wirklich Geld?
Green Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: Fr 19.05.06 06:31 
:oops: aber wie definiere ich dann unendlich? ausserdem hab ichs mit 20 versucht und da kam der gleiche fehler...
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 19.05.06 06:46 
Unendlich kann man nicht definieren.
So sollte es gehen:
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:
function TForm1.PregMatch (const AInputString,RegEx : string) : TStringDynArray;
var
  reg: TPerlRegEx;
  i: integer;
begin
  reg         := TPerlRegEx.Create(nil);
  reg.Subject := AInputString;
  reg.RegEx   := RegEx; // Der Ausdruck
  SetLength(Result,0);
  if reg.Match() then
  begin
    repeat
      //ShowMessage(reg.SubExpressions[1]);  //  0 = gesamter Match, 1 bis n = Teilmatches
      for i:=0 to reg.SubExpressionCount do
      Result[i] := reg.SubExpressions [i];
    until not reg.MatchAgain();
    Result[10]:='';
  end else begin
  Result[10]:='false';
  end;
  reg.Free();
end;

function TForm1.ParseMessage(const AParseData : string) : TStringDynArray;
  var matches,host : TStringDynArray;
begin
  SetLength(matches,11);
  SetLength(host,0);
  SetLength(Result,6);
    matches := PregMatch(AParseData , '#^(?::(\S+)\s*)?(\w+|\d{3})((?:\s+[^:\s]\S*)*)(?:\s+\:(.*))?\s*$#');
    if matches[10]='' then begin
      host := PregMatch(matches[1] , '#^(.*)!(.*)@(.*)#');
      if host[10]='' then begin
        Result[0] := host [1];
        Result[1] := host [2];[/u]
        Result[2] := host [3];
        Result[3] := '';
      end else begin
        Result[0]  := '';
        Result[1]  := '';
        Result[2]  := '';
        Result[3]  := matches[1];
      end;
    Result[4] := matches[2];
    Result[5] := matches[3];
    //SplitRegExpr('#\s+#' , Trim(matches[3]) , Result[5]);
    Result[6] := matches[4];
  end;
end;

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

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: Fr 19.05.06 18:07 
so hab das mal geändert, krieg aber immer noch ne access violation in der gleichen zeile...

ausblenden 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:
function TForm1.ParseMessage(const AParseData : string) : TStringDynArray;
  var matches,host : TStringDynArray;
begin
  SetLength(matches,11);
  SetLength(host,11);
  SetLength(Result,6);
    matches := PregMatch(AParseData , '#^(?::(\S+)\s*)?(\w+|\d{3})((?:\s+[^:\s]\S*)*)(?:\s+\:(.*))?\s*$#');
    if matches[10]='' then begin
      host := PregMatch(matches[1] , '#^(.*)!(.*)@(.*)#');
      if host[10]='' then begin
        Result[0] := host [1];
        Result[1] := host [2];
        Result[2] := host [3];
        Result[3] := '';
      end else begin
        Result[0]  := '';
        Result[1]  := '';
        Result[2]  := '';
        Result[3]  := matches[1];
      end;
    Result[4] := matches[2];
    Result[5] := matches[3];
    //SplitRegExpr('#\s+#' , Trim(matches[3]) , Result[5]);
    Result[6] := matches[4];
  end;
end;
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Fr 19.05.06 19:07 
user profile iconGreen hat folgendes geschrieben:
:oops: aber wie definiere ich dann unendlich?

Ein SetLength(Result, INFINIT) bräucht mit lauter Leerstrings "4 * INFINIT" Speicher. Und den hast du ganz sicher nicht.

_________________
Ist Zeit wirklich Geld?
Green Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: Fr 19.05.06 22:21 
hmm, aber kann mir jemand sagen warum mein code nich mag?

ich versteh den Fehler nit...
Green Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: Sa 20.05.06 11:55 
ich habs schon mit allem möglichen versucht aber es will einfach nicht.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
    matches := PregMatch(AParseData , '#^(?::(\S+)\s*)?(\w+|\d{3})((?:\s+[^:\s]\S*)*)(?:\s+\:(.*))?\s*$#');
    if matches[7]='true' then begin
      host := PregMatch(matches[1] , '#^(.*)!(.*)@(.*)#');
      if host[7]='true' then begin


geht nicht, es kommt immer der gleiche fehler...
kann mir jemand helfen??
der fehler kommt auch immer in der gleichen Zeile...
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Sa 20.05.06 17:24 
Durchsuche mal deinen Code nach weiteren "SetLength(Result,0);".

Zitat:
SetLength(matches,0);
...
matches := PregMatch(AParseData , '#^(?::(\S+)\s*)?(\w+|\d{3})((?:\s+[^:\s]\S*)*)(?:\s+\:(.*))?\s*$#');

Wenn du matches sowieso was zuweist, dann musst das dyn. Array vorher nicht auf 0 setzen, was es übrigens sowieso schon ist, wenn es sich um eine lokale Variable handelt.

_________________
Ist Zeit wirklich Geld?
Green Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: Sa 20.05.06 17:32 
wie du in meinem beitrag 4 beiträgw weiter oben siehst hab ich 'matches' bereits auf 11 gesetzt... daran kanns also nicht liegen
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Sa 20.05.06 19:13 
Das ändert auch nichts daran, dass das SetLength für matches unnötig ist, weil du mit " matches := ..." diesen Speicher gleich wieder freigibst.

Aber mir ging es hauptsächlich um das andere SetLength(Result,0) in der aufgerufenen Funktion, die dann matches zugewiesen wird.

_________________
Ist Zeit wirklich Geld?
nullplan001
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 212

Win 2000 Professional, Debian Linux 4.0 (Etch,Stable)
Pascal (FreePascal 2.0.2, TurboPascal 7.0), C(++) (G++/GCC 3.4.2 + MinGW), Java (JDK 1.5.0_07), PHP (PHP 5.1.4)
BeitragVerfasst: Sa 20.05.06 19:38 
Warum benutzt du dynamische Arrays, wenn du statische haben willst? Oder änderst du die Länge von matches später noch mal? Wäre dämlitsch. Wenn du es nicht tust, entferne die Zeile mit SetLength und definiere matches einfach als matches : array[ 0 .. 10] of string;. Das ist die überall anerkannte Methode zur Definition eines statischen Arrays. Ich empfehle, erst umzudefinieren und dann den Compiler die Zeilen mit SetLength raussuchen zu lassen. Er müsste diese rein theoretisch monieren, wenigstens als warning, weil du die (statische) Länge eines statischen Arrays ändern willst. Was ja nicht geht.
Host ist vom gleichen Typ wie Matches, Result ist vom Typ array[0..5] of string. Bei statischen Arrays kann man die Länge nicht verändern, das waren aber auch alle Unterschiede zu dynamischen Arrays. Außer vielleicht, das statische Arrays älter sind (die gab es schließlich schon bei TP1).
Tschö,
nullplan

_________________
Ich fahr' nicht selber, weil ich festgestellt habe: ich fahre zu emotional. Bin 180 gefahren wo 30 erlaubt war... -- Jürgen von der Lippe
Green Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: Sa 20.05.06 21:10 
also die länge wird später mal dynamisch, aber ich kanns jetzt auch statisch machen...
liegt noch in ferner Zukunft ^^

naja ich werds mal versuchen...