In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the application of VB.NET pointer, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Using pointers in VB is not as flexible as in C, when processing data with pointers, you need to use CopyMemory to copy data back and forth between pointers and variables that VB can handle, which requires a lot of extra overhead. So not all pointer operations in C can be transferred to VB, we should only use pointers in VB when needed.
1. Dynamic memory allocation for VB.NET pointers: impossible, possible, but not feasible, VB standard
An important reason for the frequent use of pointers in C and C++ is the need to use dynamic memory allocation, using Malloc or New to dynamically allocate memory from the stack and get a pointer to that memory. In VB, we can also use API to dynamically allocate memory and implement pointer lists like those in C.
But we can not directly use pointers to access such dynamically allocated memory like C, when we access, we must use CopyMemory to copy data into VB variables, a large number of use of this technology will inevitably reduce efficiency, so that it is not feasible to use pointers to use dynamic memory like C. To implement dynamic data structures like C # and PASCAL, we should honestly use object technology in VB.
The LinkedList in the supporting code of this article has a linked list realized entirely by pointers, which uses HeapAlloc to dynamically allocate memory from the stack, and there is a Mini Program IECache that calls the API of FindFirstUrlCacheEntry to operate IE's Cache, which uses VirtualAlloc to dynamically allocate memory. But in fact, this is not necessary, VB has provided us with a standard method of dynamic memory allocation, that is: objects, strings and byte arrays are limited to space, not to mention the technology of objects here, LinkedList source code in the linked list of useful objects, you can refer to. Strings can be allocated dynamically using the Space$ function, which is described in detail in the VB documentation. With regard to byte arrays, which we will talk about here, it is very useful. We can use Redim to dynamically resize it and pass pointers to its * elements to the API that requires pointers, as follows:
Dim ab () As Byte, ret As long 'passing a null value API returns the length of the buffer it needs. Ret = SomeApiNeedsBuffer (vbNullString) 'dynamically allocate a memory buffer of sufficient size ReDim ab (ret) As Byte' passes the pointer to API again, passing pointers to the byte array * elements. SomeApiNeedsBuffer (ByVal VarPtr (ab (1))
In IECache in the supporting program of this article, I also provide a version of dynamically allocating buffers using byte arrays, which is safer and simpler than using VirtualAlloc.
2. The application of VB.NET pointer breaks the limit.
The following is a classic application that breaks through VB type checking to achieve special functions, from Bruce Mckinney's book HardCore Visual Basic.
Extract the lower 16 bits of a Long long integer as Interger type
The standard method is also efficient, but it is not easy to understand. Function LoWord (ByVal dw As Long) As Integer If dw And & H8000 & Then LoWord = dw Or & HFFFF0000 Else LoWord = dw And & HFFFF& End If End Function 'using pointers is not efficient, but it is clear. Function LoWord (ByVal dw As Long) As Integer CopyMemory ByVal VarPtr (LoWord), ByVal VarPtr (dw), 2 End Function Thank you for reading this article carefully. I hope the article "how to use VB.NET pointers" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.