[fpc-pascal] dynamic array contents and system.move
graemeg.lists at gmail.com
Wed May 5 08:54:01 CEST 2010
José Mejuto het geskryf:
> architectures. Much better would be "move
> (array1,array2,elements*sizeof(element))" but even in this case
> the result is undefined, but it is a bit more exact as the programmer
Now this is a perfect example, of why I love Java (at least what I have
seen of it - I am no Java expert). In your example, the developer need to
have knowledge of the internal structure of an array before they can use
the move() function. Java and good programming practices like OOP states
that you should try and hide the internal structure from a client using
your structure. This gives you the freedom to change the internal structure
without the need to change client code. This is the reason why things like
Iterator exists in design patterns.
What if move() and the developer assumes the data is one continuous block
of data, when in fact it might not be. [I don't know internal structures of
FPC] Seeing that dynamic arrays are a collection of pointers (I think I
understood this correctly), each memory block pointed to by each pointer
could be in a totally different memory location. So copying a continuous
block of memory will result in rubbish data as a result.
--------[ Extract from Java Documentation ]------------
static void arraycopy(Object src, int src_position, Object dst,
int dst_position, int length)
Copies an array from the specified source array, beginning at the specified
position, to the specified position of the destination array.
// playerList is existing array
int newSize = 2 * playerList.length; // Size of new array.
Player temp = new Player[newSize]; // The new array.
System.arraycopy(playerList, 0, temp, 0, playerList.length);
Note: No need to know internal data structures like an array item is
actually a pointer to the actual data in memory etc... Much easier to work
Requirements like having to know the internal structure, simply to be able
to use that structure is not a cleaver design practise. But lucky for me, I
hardly ever use arrays - I much prefer classes. ;-)
Maybe the solution is as simply as introducing a new function in FPC called
arraycopy(), which can then hide the complexity of internal data structures?
As always, just my 2c worth of info. Use it, don't use it - I don't care.
- Graeme -
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
More information about the fpc-pascal