Einzelnen Beitrag anzeigen

Medium

Registriert seit: 23. Jan 2008
3.679 Beiträge
 
Delphi 2007 Enterprise
 
#13

AW: How to reverse array of any type?

  Alt 3. Dez 2012, 11:04
You need to understand, that you can not operate in a array-like fashion anymore, if you want a fully generic function. You have to go down an abstraction layer, and work on the actual memory.
An array simply is a sequence of values written behind each other in memory. Every element has a length (in bytes), but the length will be determined by the data type in the array. You employ pointer operations instead of the compiler magic (pascal arrays) to gain the possibility to work with data of arbitrary size. Without Generics, there just is no other way. So instead of array indexes, you point to elements using a memory address and the size of the elements. Traversing one element ahead "increase index by 1" then becomes "take pointer of the current element, and add the element-size to it". Copying "myArray[i] := myArray[m]" becomes a raw memory move-operation "Move(DestinationPointer, SourcePointer, ElementSizeInBytes)". As long as you have anything with square brackets "[]" in your reverse-function, you can be sure to think too high-level
"When one person suffers from a delusion, it is called insanity. When a million people suffer from a delusion, it is called religion." (Richard Dawkins)
  Mit Zitat antworten Zitat