VB6 UDT 中固定长度字符串与可变长度字符串的处理方式有何不同?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,“作为字符串”在 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.