Results 1 to 3 of 3

Thread: function returns an unitialized int64?

  1. #1

    function returns an unitialized int64?

    Delphi 2005 Pro
    TypeCasting
    Function
    Int64

    Hello i have a function where i use
    result:=0;
    then i loop trough a list and update result if i find a match.

    Now when calling the function i expect 0 then a match is not found instead i get 18...........etc as a result which means an uninitialized int64. Why? I believe last week when i tested it i got 0. I do a typecast in calling the function e.g. it is in another class that is passed as an object to the current class. E.g. the code is findfile that is called from TVirtualFileStream.Create . Code can be found in vfs.

    [EDIT]
    I Just discovered that on setting a breakpoint on result:=0; the breakpoint is ignored as the compiler seems to not compile the line (due to optimizations). Why?
    [/EDIT]
    http://3das.noeska.com - create adventure games without programming

  2. #2

    function returns an unitialized int64?

    Hi noeska,
    can you show us the actual routine?
    We may be able to help you more.

    cheers,
    Paul

  3. #3

    function returns an unitialized int64?

    Here it is:

    Code:
    {$O-}
    function TVirtualFileSystem.FindFile(aname: string; var afilerecord: TFileRecord): int64;
    var
      FileRecordEntry: TFileRecord;
      buffer: pointer;
      i,m,size: integer;
      foundpos: integer;
      OrigFilePos: int64;
    begin
    
      result :=0;
    
      FileRecordEntry.filename := 'Not Found';
      FileRecordEntry.filesize := 0;
      FileRecordEntry.startblock := 0;
      afilerecord :=FileRecordEntry;
      foundpos:=0;
      if aname = '/' then
      begin
        //read first dir entry
        //TODO: can this be simplified by assuming a fixed /
        self.ReadBlock(1,FileRecordEntry,0,SizeOf(TFileRecord));
    
        //compare ( is the first entry realy an / )
        if FileRecordEntry.filename = aname then
        begin
          result := foundpos * SizeOf(TFileRecord);
          afilerecord := FileRecordEntry;
        end;
      end
      else
      begin
        //browse through FileRecord(s) in virtual file stream
        OrigFilePos:=FDir.FPosition;
        FDir.FPosition :=0;
        i:=0;
        m:= (FDir.Size div SizeOf(TFileRecord));
        while i<=m-1 do
        begin
          FDir.Read&#40;FileRecordEntry, SizeOf&#40;TFileRecord&#41; &#41;;
          //compare
          if FileRecordEntry.filename = aname then
          begin
            result &#58;= FDir.FPosition-SizeOf&#40;TFileRecord&#41; ;
            afilerecord &#58;= FileRecordEntry;
            FDir.FPosition &#58;=OrigFilePos;
            break;
          end;
          i&#58;=i+1;
        end;
        FDir.FPosition &#58;=OrigFilePos;
      end;
    end;
    &#123;$O+&#125;
    and calling:
    Code:
    &#123;$O-&#125;
    constructor TvfsFileStream.Create&#40;aname&#58; string; aVFSFileSystem&#58; TObject&#41;;
    var
      FileRecordPos&#58; int64;
    begin
    
      FileRecordPos &#58;= 0;
      inherited Create&#40;&#41;;
            FBlockList&#58;=nil;
    
      //determine if aVFSFileSystem is a TVirtualFileSystem
      if aVFSFileSystem <nil> 0 then
          begin
            //TODO&#58; overwrite current file &#40;e.g. delete it dependend on fmode&#41;
            FSize&#58;=FFileRecord.filesize;
            FFirstBlock &#58;= FFileRecord.startblock;
          end
          else
          begin
            //new file
            FFirstBlock &#58;= TVirtualFileSystem&#40;self.FDataFileP&#41;.NextFreeBlock&#40;&#41;;
            TVirtualFileSystem&#40;self.FDataFileP&#41;.AddFile&#40;aname,FFirstBlock , 0&#41;;
            FSize&#58;=0;
          end;
    
          FPosition&#58;=0; //go to start of virtual file
    
          //determine what blocks are used by virtual file &#40;if any&#41;
          FBlockList&#58;=TVirtualFileSystem&#40;self.FDataFileP&#41;.GetBlockList&#40;FFileRecord&#41;;
    
          //some more for making a new virtual file
          if FFilerecord.filename = 'Not Found' then
          begin
            FFileRecord.filename &#58;= aname;
            FFileRecord.startblock &#58;=TVirtualFileSystem&#40;self.FDataFileP&#41;.NextFreeBlock&#40;&#41;;
          end;
          FPosition&#58;=0; //go back to start of virtual file
        end
        else
          Raise Exception.Create&#40;'aVFSFileSystem must be a TVirtualFileSystem'&#41;;
    
    end;
    &#123;$O+&#125;
    Now with {$O-} the lines do execute.

    But as it turned out the cause of the program malfunctioning was due to another here unrelated part. I even wrote a comment there that i might not work ops: .

    Now i think i can also remove the {$O-} again as the optimizer is clever enough not to break things only the breakpoints go slightly wrong.
    http://3das.noeska.com - create adventure games without programming

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Comodo SSL