11

I have such a basic problem in Delphi,I can't solve it.

My Code:

Note:DataR is local in the methods below,but usually it's a class var.Just for the concept it's local.

class procedure TCelebrity.BeginRead(var input:Array of byte);
var DataR:Array of byte;
begin
  VirtualFree(@DataRead,High(DataRead),MEM_RELEASE);
  SetLength(DataR,Length(input));
  Move(input,DataR,Length(input));
end;

This compiles,but after Move() is executed DataR = nil.

Second try:

class procedure TCelebrity.BeginRead(var input:Array of byte);
var DataR:Array of byte;
begin
  VirtualFree(@DataRead,High(DataRead),MEM_RELEASE);
  SetLength(DataR,Length(input));
  DataR := Copy(input,0,Length(input));
end;

This doesn't compile at all.Error at the third line(DataR := Copy(input....) saying "Incompatible types".

Where's the problem? They are all Array of byte!

1

4 Answers 4

9

try this

type
  TByteDynArray = array of Byte;

function CopyData(const Input:array of Byte):TByteDynArray;
begin
  SetLength(Result, Length(Input));
  Move(input[0], Result[0], Length(Input));
end;
Sign up to request clarification or add additional context in comments.

4 Comments

A correct answer. but to make it a good answer, you should explain why your changes are significant. Otherwise, it's just a magical incantation.
Correct me if I'm wrong, but it might be worth noting that this only works as written for array of Byte. If the array was anything different, say array of Integer, then you'd need Move(input[0], Result[0], Length(Input) * SizeOf(Integer)). Just saying.
@Rob Kennedy: Move() in delphi is similar to memcpy()/memmove() in C
I know that, @Dns. So what?
8

Why not use FOR?

SetLength(DataR,Length(input));
for i:=Low(input) to High(input) do
  DataR[i]:=input[i];

BTW: if you want to have arrays passing as parameter, you should declare them as a type, eg:

type
  TMyArray = array of byte;

and use TMyArray as parameters type.

Edit: I was notified about i lower value. In my original post it was for i:=0, but i:=Low(input) is safer and more pure.

5 Comments

I have one question.The VirtualFree always returns false so how do I free the mem currently used with DataR? Note DataR is not local,but class var in the original code.Thank you in advance for the answer!
No, you do not have free simple arrays. Such arrays are freed automatically (for example when your class is being destroyed). Note: if you have an array of references to objects, you have to free those objects manually, because only array will be freed by Delphi.
Also note, that SetLenght reallocates the whole array, so avoid intensive usage of this procedure. When you make the array longer with SetLength, the current content remains intact, however, when you make it shorter, some of the content will be lost.
In answer to 'Why not use FOR?'. My only reason would be if there was a real performance problem. If you were doing this a serious number of times, then Move would be faster, I reckon.
use Move() it's similar to memcpy()/memmove() in C. Never use FOR to copy array
2

Try:

class procedure TCelebrity.BeginRead(var input:Array of byte);
var DataR:Array of byte;
begin
  VirtualFree(@DataRead,High(DataRead),MEM_RELEASE);
  SetLength(DataR,Length(input));
  Move(input[0],DataR,Length(input));
end;

1 Comment

The VirtualFree() call seems a bit superfluous.
1

Move procedure will not move a section of memory. It copies Count bytes. Hereby you will get two different identical arrays: Input and DataR.

procedure CopyData(const Input: Array of Byte);
var 
    DataR: Array of Byte;
begin
    SetLength(DataR, Length(Input));
    Move(Input[0], DataR[0], SizeOf(Byte)*Length(Input));
end;

P.s. with static arrays you can use SizeOf(Input) instead of SizeOf(Byte)*Length(Input). Instead of Byte could be other data type.

1 Comment

why not use SizeOf(Input[0]) instead of a hardcoded data-type?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.