TArray< Byte>VS TBytes VS PByteArray [英] TArray<Byte> VS TBytes VS PByteArray

查看:72
本文介绍了TArray< Byte>VS TBytes VS PByteArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这3种类型非常相似...

TArray是TBytes的通用版本.两者都可以转换为PByteArray并用作Windows API调用的缓冲区.(与Pchar的字符串具有相同的限制).

我想知道的是:此行为是设计使然"还是通过实施".或者更具体地说,它会在将来的版本中中断吗?

//编辑如前所述...我真正想知道的是:就向前兼容性而言,将TBytes(或TArray)类型转换为PByteArray是否像将String类型转换为PChar一样安全?(或者也许将AnsiString替换为PAnsiChar是一个更好的例子^ _ ^)

解决方案

简单地说,字节数组就是字节数组,只要字节和数组的定义不变,就不会也不要改变.只要确保确保遵守数组边界,就可以安全地使用它,因为将其从Delphi的数组类型中剔除将使边界检查无效.

我想我明白您的要求了.

否,您不应该将动态数组引用转换为C样式的数组指针.您可以使用字符串来摆脱它,因为编译器可以为您提供一些帮助.

但是,您可以做的是将指向动态数组元素0的指针转换为C样式数组指针.起作用,并且不会改变.

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 ^_^)

解决方案

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.

这篇关于TArray< Byte>VS TBytes VS PByteArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆