Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TArray<Byte> VS TBytes VS PByteArray

Those 3 types are very similar...

TArray is the generic version of TBytes. Both can be casted to PByteArray and used as buffer for calls to Windows API. (with the same restrictions as string to Pchar).

What I would like to know: Is this behavior "by design" or "By Implementation". Or more specifically, could it break in future release?

//Edit As stated lower... What I really want to know is: Is this as safe to typecast TBytes(or TArray) to PByteArray as it is to typecast String to PChar as far as forward compatibility is concerned. (Or maybe AnsiString to PAnsiChar is a better exemple ^_^)

like image 879
Ken Bourassa Avatar asked Mar 11 '10 18:03

Ken Bourassa


1 Answers

Simply put, an array of bytes is an array of bytes, and as long as the definitions of a byte and an array don't change, this won't change either. You're safe to use it that way, as long as you make sure to respect the array bounds, since casting it out of Delphi's array types nullifies your bounds checking.

EDIT: I think I see what you're asking a bit better now.

No, you shouldn't cast a dynamic array reference to a C-style array pointer. You can get away with it with strings because the compiler helps you out a little.

What you can do, though, is cast a pointer to element 0 of the dynamic array to a C-style array pointer. That will work, and won't change.

like image 194
Mason Wheeler Avatar answered Sep 27 '22 18:09

Mason Wheeler