In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to calculate the size of Marshal.SizeOf". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to calculate the size of Marshal.SizeOf".
A strange phenomenon of C# was found today:
String [] msgs = {"zs", "li", "wu"}
Int sizeoflen = Marshal.SizeOf (msgs)
IntPtr pmsgs = Marshal.AllocHGlobal (sizeoflen)
It turns out that the sentence Marshal.SizeOf (msgs) appears: an unhandled exception of type ArgumentException occurs in mscorlib.dll, and other information: String [] cannot be marshaled as an unmanaged structure: meaningful size or offset cannot be calculated. How to replace it with int sizeoflen = Marshal.SizeOf (msgs [0]); it is also an exception: String cannot be marshaled as an unmanaged structure: meaningful size or offset cannot be calculated.
But if there is:
[StructLayout (LayoutKind.Sequential)]
Class CLS
{
Public int id
Public float value
[MarshalAs (UnmanagedType.ByValArray,SizeConst=10,ArraySubType=UnmanagedType.Struct)]
Public Point [] ps
[MarshalAs (UnmanagedType.LPStr)]
Public string msg
[MarshalAs (UnmanagedType.ByValArray,SizeConst=5,ArraySubType=UnmanagedType.LPStr)]
Public string [] msgs
}
CLS [] clss = new CLS [2]
Int clsslen = Marshal.SizeOf (clss)
A CLS [] exception similar to the above occurs; modified to
Int clsslen = Marshal.SizeOf (clss [0]); an unhandled exception of type AugumentNullException: the value cannot be null. Change it again to
Clss [0] = new CLS ()
Clss [1] = new CLS ()
Int clsslen = Marshal.SizeOf (clss); again: CLS [] exception; and then modified to target a member object:
Int clsslen = Marshal.SizeOf (clss [0]); the value size occurs successfully.
This means that to calculate the true size of the CLS [] clss, you can multiply the size of a member by the number of elements.
Going back to the original string [] msgs = {"zs", "li", "wu"}; in the end, the experiment found that neither Marshal.SizeOf (msgs) nor Marshal.SizeOf (msgs [0]) could calculate the size of the exception. It can be concluded that:
String or string [] to calculate the size, it is impossible to get the result through Marshal.SizeOf.
Others, including the struct value type (struct is qualified by the StructLayout attribute), can be obtained through Marshal.SizeOf, and the underlying value type can be obtained with sizeof; reference types can be obtained through Marshal.SizeOf, but the array cannot be obtained directly, you need to initialize the members of the array first, and then the result of Marshal.SizeOf (a member object variable) multiplied by the length of the array is the total memory size.
Thank you for your reading, the above is the content of "how to calculate the size of Marshal.SizeOf". After the study of this article, I believe you have a deeper understanding of how to calculate the size of Marshal.SizeOf, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 210
*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.