Difference between revisions of "Talk:Dynamic array"

From Lazarus wiki
Jump to navigationJump to search
Line 1: Line 1:
I was a little bit confused about the sentence "''To init such an invalid pointer you have to use '''FillChar(MyVariable,sizeof(MyVariable), #0)'''.''" This might be misleading, as this fills the '''pointer''' to MyVariable with the size of this pointer with zeros, not the array itself, as far as I understand. This might be different to static arrays (didn't prove that).  
+
I was a little bit confused about the sentence "''To init such an invalid pointer you have to use '''FillChar(MyVariable,sizeof(MyVariable), #0)'''.''" This might be misleading, as this fills the '''pointer''' to MyVariable with the size of this pointer with zeros, not the array itself, as far as I understand. This might be different to static arrays (didn't prove that). Logically, the sentence is right. but it's anyway confusing, because the instruction does not, what it seems to do in human language: FillChar doesn't fill My(Array)Variable, and SizeOf doesn't return the size of MyVariable...
  
Anyway: It took me some time to find out, how to initialize the array itself:
+
It took me some time to understand this. Then it's not very hard to find out, how to initialize the array itself:
 
FillChar(MyVariable[0], Length(MyVariable) * SizeOf(TheArrayType), #0);
 
FillChar(MyVariable[0], Length(MyVariable) * SizeOf(TheArrayType), #0);
  
Perhaps this difference between initializing the array itself and the reference to the array should be emphasized? Or did I misunderstand something there?
+
So the problem is mainly the terminology, which puts you in the wrong trace. Perhaps this difference between initializing the array itself and the reference to the array should be emphasized? Or did I misunderstand something there?

Revision as of 12:32, 11 August 2014

I was a little bit confused about the sentence "To init such an invalid pointer you have to use FillChar(MyVariable,sizeof(MyVariable), #0)." This might be misleading, as this fills the pointer to MyVariable with the size of this pointer with zeros, not the array itself, as far as I understand. This might be different to static arrays (didn't prove that). Logically, the sentence is right. but it's anyway confusing, because the instruction does not, what it seems to do in human language: FillChar doesn't fill My(Array)Variable, and SizeOf doesn't return the size of MyVariable...

It took me some time to understand this. Then it's not very hard to find out, how to initialize the array itself: FillChar(MyVariable[0], Length(MyVariable) * SizeOf(TheArrayType), #0);

So the problem is mainly the terminology, which puts you in the wrong trace. Perhaps this difference between initializing the array itself and the reference to the array should be emphasized? Or did I misunderstand something there?