Frank Heckenbach replied:
are particular Pascal structures that shouldn't be zeroed by this technique (apart from dynamic arrays)?
Any kind of schemata, which include dynamic arrays ans strings
Also files and objects.
There is a built-in procedure `Initialize' to (re-)initialize any variable, so you could theoretically zero out everything, then initialize it again, but I'm not sure if I should have mentioned this. ;-) In any case, you'll do some extra work (init, zero, init again) ...
But I agree, in this case I'd recommend a `for' loop, too.
PROCEDURE zero(VAR param; ch : char; size : longint); asmname 'memset';
BEGIN NEW( Test1, a ); writeln(sizeof(Test1^)); zero(Test1^,#0,sizeof(Test1^)); END.
OK - that's helpful. My example was trivial, but in practice I'm trying to zero very large arrays of large records which I very time consuming with loops and type-matched assignments.
So I do new(anyvar,x, y); zero(anyvar^,#0,sizeof(anyvar)); initialize(anyvar);
.. and because initialize is a built-in, it doesn't need any declaring. BUT ... when I run with electric-fence I get a SIGSEGV in memset. Doing some experimentation, it appears that each dynamic array dimension uses an extra 4 words. So if I amend my zero function call as follows ...
zero(anyvar^,#0,sizeof(anyvar)-8);
... the problem goes away. In this case, the Initialize() call doesn't seem to make any difference.
Am I right in assuming that the dynamic array sizing is tacked on the end of the storage so deducting 4 words per dimension is the right approach?
Is Initialize() only required after I've zeroed a string or a record containing a string?
Finally, given that Initialize can very usefully take any variable as a parameter, it's a shame that it doesn't actually do the zeroing. Is there another built-in I haven't spotted that will do this without be having to explicitly worry about the number of dimensions the dynamic arrays have?
David Wood, QinetiQ Farnborough.
The Information contained in this E-Mail and any subsequent correspondence is private and is intended solely for the intended recipient(s). For those other than the recipient any disclosure, copying, distribution, or any action taken or omitted to be taken in reliance on such information is prohibited and may be unlawful.