PDA

View Full Version : function returns an unitialized int64?



noeska
08-12-2008, 07:52 PM
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.


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?

paul_nicholls
08-12-2008, 09:56 PM
Hi noeska,
can you show us the actual routine?
We may be able to help you more.

cheers,
Paul

noeska
09-12-2008, 05:52 PM
Here it is:



{$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(TFileRec ord));

//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:


&#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;.Ge tBlockList&#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;.NextFreeBloc k&#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 :oops: .

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.