I'm not certain but I'm pretty sure that under 1.0.6 SizeOf() reported the proper size for Var paramaters. If not then I think a compiler warning may be in order for code like this... because I just spent an hour tracking down this problem. ;) Is there any other way of determining the original size of the var param?
Mailing lists / fpc-devel / 2003-07
SizeOf() reports 0 for Var Paramaters. (1.1)
8 messages · 4 people ·
- JJon Sawyer 3
- MMarco van de Voort 3
- IIvan Shikhalev 1
- NNikolay Nikolov 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hello, Jon!
You wrote to ""
on Sun, 27 Jul 2003 01:32:39 -0400:
SizeOf - is a compiler-time defined function. If it will return a true value in run-time, the compiler must include in .exe all info about all variables and expressions.
In Delphi or TP the SizeOf function is same.
------------
Ivan Shikhalev
http://fpc.by.ru/
I'm not certain but I'm pretty sure that under 1.0.6 SizeOf() reported the proper size…
Show quoted text 5 lines
IIRC it returns correct data for _typed_ open arrays, not for "generic" open arrays.
So for
proceduer bla(var a: array of longint);
// high() and sizeof() are possibleand for
procedure bla(var a);
// none.I'm pretty certain it was the same for 1.0.6. This was already the case in BP/TP (e.g. see the blockwrite and blockread procedures that require the length to be explicitely passed)
O_o Woah.. Weird. Ok.. I thought it worked.. but mabey not. 1.0.6 seems to return 4 for SizeOf() a var param. While 1.1 returns 0.
Progam Test1;
Procedure DoTest(var A);
Begin
Writeln('SizeOf A = ', SizeOf(A) );
end;
var
A : QWord;
Begin
A := 2390;
Writeln('SizeOf A = ', SizeOf(A) );
DoTest(A);
end.On 1.0.6 it outputs:
SizeOf A = 8
SizeOf A = 4
On 1.1 it Outputs:
SizeOf A = 8
SizeOf A = 0
Marco van de Voort wrote 26 lines
IIRC it returns correct data for _typed_ open arrays, not for "generic" open arrays. So…
Marco van de Voort wrote 13 lines
Fpc 1.0.6's SizeOf used to report 4 (regardless of the size of the parameter actually passed) and I submitted a bug report, so it was fixed: http://www.freepascal.org/bugs/showrec.php3?ID=2561 http://www.freepascal.org/bugs/showrec.php3?ID=2562
Show quoted text 16 lines
1.0.10 also report 4. 1.1 reports 0.
parameter actually passed) and I submitted a bug report, so it was fixed: http://www.freepascal.org/bugs/showrec.php3?ID=2561 http://www.freepascal.org/bugs/showrec.php3?ID=2562
These say that it is fixed in 1.1, and that's true, I verified this.
Probably Peter only fixed it in 1.1, because he considered it a minor bug ( code that relies on this is IMHO severly broken).
Show quoted text 8 lines
Funnily enought all the times I tested it on 1.0.6 I was using DWords and Pointers.. I tried my array w/ Bytes/Words and never had any problems (because my array simply stored the var and just had a few extra padding bytes) I never really ran into the problem.
I just noticed now w/ 1.1 because nomater what Index I gave my array it allwasy returned the same number, Later I tracked it down to the array thinking all the items were of 0 size. ie: iIndex*iItemSize = 0. I've fixed the problem by requireing the programer to pass the size of his item in Create() but I think if it is possible, that would be a nifty feature.
O_o Woah.. Weird. Ok.. I thought it worked.. but mabey not. 1.0.6 seems to return 4 for…
O_o Woah.. Weird. Ok.. I thought it worked.. but mabey not. 1.0.6 seems to return 4 for SizeOf() a var param. While 1.1 returns 0.
It can't work, simply because it is impossible for the procedure to get the size of such a parameter (incl the const variant). The compiler can't know it when compiling (several routines could call this routine with different sizes), and there is no runtime info passed.
In general:
procedure dotest (var a); // const case is the same -> only pointer to "a" is passed. No way to determine type, number of elements or #bytes.
procedure dotest (var a: array of longint);
-> pointer + number of elements is passed.procedure dotect (const a: array of const); // delphi construct -> 1. a tabel with a pointer and a typecode for each item is created on the stack
2. a pointer to that item+ number of items is pushed onto the stack
The last one needs to be called like
dotect([param1,param2,param3]);