代码之家  ›  专栏  ›  技术社区  ›  john_who_is_doe

求值器中的表达式非法(访问冲突)(&A)

  •  -2
  • john_who_is_doe  · 技术社区  · 11 年前

    我试图通过使用类Tb2uc处理一个纯文本文件(加载到StringList中),但在调用函数GetAddress时获得AV。

      TArrayQuotePositions = array[1..4] of integer;
    
      Tbb2uc = class(TObject)
      private
        Farrayquotes: TArrayQuotePositions;
        SlInput: TStringList;
        Inputfilename: TFileName;
        SlOutput: TStringList;
        function GetQuotePositions( aLine: string ): TArrayQuotePositions;
        function GetInvoice( aLine: string ): string;
        function GetName( aLine: string ): string;
        function GetAddress( aLine: string ): string;
        function GetSwift( aLine: string ): string;
        function ProcessSl: integer;        
      public
        constructor Create;
        destructor Destroy; override;
        function OpenFile: integer;
      end;
    

    function Tbb2uc.GetInvoice( aLine: string ): string;
    var
      quotesPos: TArrayQuotePositions;
    begin
      quotesPos := GetQuotePositions( aLine );
      result := copy( aLine, quotesPos[3]+1, (quotesPos[4]-quotesPos[3])-1 );
    end;
    

    function Tbb2uc.GetName( aLine: string ): string;  
    var
      quotesPos: TArrayQuotePositions;
    begin
      quotesPos := GetQuotePositions( aLine );
      result := copy( aLine, quotesPos[3]+1, (quotesPos[4]-quotesPos[3])-1 );
    end;
    

    执行甚至不会跳到函数中。我在调用此功能时获得AV。

    function Tbb2uc.GetAddress( aLine: string ): string;
    var
      quotesPos: TArrayQuotePositions;
      address1: string;
      address2: string;
    begin
      quotesPos := GetQuotePositions( aLine );
      address1 := copy( aLine, quotesPos[1]+1, (quotesPos[2]-quotesPos[1])-1 );
      address2 := copy( aLine, quotesPos[3]+1, (quotesPos[4]-quotesPos[3])-1 );
      result := address1 + ' ' + address2;
    end;
    

    使用上述功能:

    function Tbb2uc.ProcessSl: integer;
    var
      i: integer;
      line: string;
      invoice,name,addr,swift: string;
    begin
      SlInput.LoadFromFile( string(Inputfilename) );
      //
      for i := 0 to SlInput.Count -1 do begin
        if ansipos( STARTSTRING, SlInput[i]) <> 0 then begin
          invoice := GetInvoice(SlInput[i]);
          name := GetName(SlInput[i+1]);
          ////
          addr := GetAddress(SlInput[i+2]); //Access Violation
          ////
          swift := GetSwift(SlInput[i+4]);
          line := line + invoice + ';' + name + ';' + addr + ';' + swift;
        end;
        SlOutput.Add(line);
        line := '';
      end;
      SlOutput.SaveToFile( OUTPUT_FNAME );
    end;
    

    在调用GetAddress之前,执行正常。在调试时,当对SlInput[i+2]求值时,我得到了“求值器中的表达式非法”,现在我不知道。如您所见,我只处理字符串列表的行。

    2 回复  |  直到 11 年前
        1
  •  4
  •   J...    11 年前

    您正在访问字符串列表。

    for i := 0 to SlInput.Count -1 do begin
    
     // ...(etc)
    
          addr := GetAddress(SlInput[i+2]); 
    

    i 正在运行到列表的全部大小。你的索引比这个高两个。

        2
  •  -1
  •   john_who_is_doe    11 年前

    这是一个静态数组,我正在过度索引。调试器无法精确定位精确的行,因此(对我来说)不明显:

    function Tbb2uc.GetQuotePositions( aLine: string ): TArrayQuotePositions;
    var
      i: integer;
      arraypos: integer;
    begin
      for i := low(TArrayQuotePositions) to high(TArrayQuotePositions) do begin
        Farrayquotes[i] := 0;
      end;
      arraypos := low(TArrayQuotePositions);
      for i := 1 to length(aLine) do begin
        if aLine[i] = CHAR_FOR_COLLECT then begin         
          Farrayquotes[arraypos] := i;  //over-indexing, Farrayquotes: TArrayQuotePositions = array[1..4] of integer;
          inc(arraypos);            
        end;
      end;
      result := Farrayquotes;
    end;
    

    抱歉……我的问题还远远没有完成。