[fpc-pascal] Array clearing
juergen.hestermann at gmx.de
Wed Apr 12 16:07:23 CEST 2017
Am 2017-04-12 um 15:05 schrieb noreply at z505.com:
> Since fillchar is not a general way to clear an item
Define "clear". There are many things that can be named "clearing" (see below).
> I've never been a fan of things like fillchar as it seems like a reinvention of something more like Clear() and mistakes can easily be made when you use complex function like this:
> fillchar(something, something * something, 0)
> instead of a more simpler:
It's true that the wording "char" is not ideal.
Instead you can use FillByte ( http://www.freepascal.org/docs-html/rtl/system/fillbyte.html )
which does the same (it has just another name).
FillByte does what it says: It fills a given memory area with bytes (which you can define).
Because of the nature of certain types it can damage i.e. managed types
so you have to know what you do (you have to know about the memory usage of the used type).
If you have a static array things are completely different as for dynamic arrays.
If you have a static array the memory it occupies is static (never changes) and
in one continuous block.
Dynamic arrays are completely different although their syntax is (nearly) the same.
So what should Array.Clear do in case of a static and in case of a dynamic array?
Clearing a static array can only mean filling it with bytes.
But do they have to be zero in all cases?
Array of integer may require different bytes than array of char.
In the end you as the programmer have to decide what is appropiate.
Array.Clear would not be able to guess what you intend.
Clearing a dynamic array can mean to free all memory it occupies
or fill its used memory with bytes.
Again you have to decide what to do
and which bytes should be used (if you fill everything).
> To me, fillchar doesn't actually say anything sensible about what the code is doing...
Well, if you use FillByte (instead of FillChar) then nothing can be clearer. It just fills memory with a chosen byte.
> "Clear" or "Default" (I think delphi is implementing something like this in later versions) says exactly what the code is doing.
What exactly does it do when "clearing" a dynamic array?
There are multiple ways to "clear" it.
> FillChar is some obscure old 1970's/80's pascal function that really doesn't tell what you are doing, unless you are an old pascal programmer who knows it from long ago.
It's true that those who grow up with it know that FillChar is actually a Fillbyte.
The first versions of FillChar just excepted the char type as the fill byte.
> I'm skeptical of these old ways of doing things.. Imagine a newcomer to pascal who did not program in the 1980's... He sees "fillchar" in the program...
> Does that code even tell this learner of pascal what is happening? What's a "char" have to do with an array that may not have anything to do with chars?
That applies to all Pascal functions.
You need to lookup what they actually do.
In case of FillChar/FillByte it is quite easy to unterstand.
Much more problematic are things that the compiler does
"under the hood" which is often not well documented.
If you don't know how the compiler handles dynamic arrays
you have a real problem as you cannot predict performance
nor do you know exactly how to handle them.
But that has nothing to do with FillChar...
More information about the fpc-pascal