VB6 UDT 中固定长度字符串与可变长度字符串的处理方式有何不同?

发布于 2024-10-28 07:04:07 字数 949 浏览 1 评论 0原文

在 VB6 中,我可以在 UDT 中使用固定或可变长度字符串:

Public Type MyRecord
    VariableLengthString As String
    FixedLengthString As String * 80
End Type

我希望 FixedLengthString 在 UDT 中分配 160 个字节,如果我有这些 UDT 的数组,则固定长度字符串数据是内联的。

VariableLengthString 发生了什么?是在堆上分配的吗?

因此,如果我这样做:

Dim record1 As MyRecord
Dim record2 As MyRecord
record1.VariableLengthString = "a"
record2 = record1

...显然 record2.VariableLengthString 将是 "a"。但是,如果我这样做:

record2.VariableLengthString = "b"

...那么 record1.VariableLengthString 的值是多少?

更新:结果发现结果仍然是"a",也就是说字符串被复制了。那么不是在堆上吗?

我的测试代码:

record1.VariableLengthString = "a"
record2 = record1
? record2.VariableLengthString
a
record2.VariableLengthString = "b"
? record1.VariableLengthString
a

In VB6, I can use either fixed or variable length strings in a UDT:

Public Type MyRecord
    VariableLengthString As String
    FixedLengthString As String * 80
End Type

I would expect FixedLengthString to allocate 160 bytes in the UDT, and if I had an array of these UDT's, the fixed length string data is inline.

What's going on with VariableLengthString? Is it allocated on the heap?

Therefore, if I do this:

Dim record1 As MyRecord
Dim record2 As MyRecord
record1.VariableLengthString = "a"
record2 = record1

... obviously record2.VariableLengthString will be "a". But, if I do this:

record2.VariableLengthString = "b"

... then what is the value of record1.VariableLengthString?

Update: It turns out that the result is still "a", which means the string is copied. So is it not on the heap?

My test code:

record1.VariableLengthString = "a"
record2 = record1
? record2.VariableLengthString
a
record2.VariableLengthString = "b"
? record1.VariableLengthString
a

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生活了然无味 2024-11-04 07:04:07

基本上,“作为字符串”在 VB6 中分配 BSTR。 OLE(VB6 的基础技术)以特殊方式处理 BSTR,包括维护缓存以使字符串操作更快。 MSDN 中的这篇有关字符串操作的文章对此进行了更详细的解释。 UDT 指向 BSTR 结构,该结构基本上是一个 32 位长度字段,后跟字符串的字符。它支持 Unicode。分配给 BSTR 的空间是 4 个字节 + 存储字符的字节数。所以它是可变的,不像固定长度字符串那样固定。

Basically "as string" allocates a BSTR in VB6. OLE (the technology underlying VB6) handles BSTRs in special ways including maintaining a cache to make string manipulation faster. This article on String Manipulation in MSDN explains it more detail. The UDT points to a BSTR stucture that is basically a 32bit length field followed by the characters of the string. It is Unicode aware. The space allocated to a BSTR is 4 bytes + the number of bytes to store the characters. So it is variable not fixed like Fixed Length String.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文