473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Structure to Byte Array - Which is better? Marshal Copy or FixedByte Assignment

I have a structure named EntityState with an explicit layout.
The following two operations exist within the class to return a byte
array representing the current object. Upon executing them each a
million times, I've learned that the ToRaw2() operation is twice as fast
as ToRaw().

So is it always safe to use the ToRaw2() operation or is there some gain
in using Marshal's Copy operation?
unsafe public byte[] ToRaw()
{
byte[] byteArray = new byte[EntityState.PDU_SIZE];
IntPtr pointer = Marshal.AllocHGlobal(EntityState.PDU_SIZE);
Marshal.StructureToPtr(this, pointer, false);
Marshal.Copy(pointer, byteArray, 0, EntityState.PDU_SIZE);
Marshal.FreeHGlobal(pointer);
return byteArray;
}

unsafe public byte[] ToRaw2()
{
byte[] byteArray = new byte[EntityState.PDU_SIZE];
fixed (byte* ptr = byteArray)
{
*((EntityState*)ptr) = this;
}
return byteArray;
}
Feb 4 '07 #1
2 7588
>So is it always safe to use the ToRaw2() operation or is there some gain
in using Marshal's Copy operation?
ToRaw will handle a larger variety of structs. You can only use
pointers on a very restricted set of types in C#. But as long as it
works, it's fine to use ToRaw2.

If performance is your top priority then I guess ToRaw2 is the better
alternative. But I would also consider a third alternative that used
the BitConverter class to convert each member separately. The benefit
there is that the code doesn't require full trust to run.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 4 '07 #2
On Feb 4, 6:21 am, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
So is it always safe to use the ToRaw2() operation or is there some gain
in using Marshal's Copy operation?

ToRaw will handle a larger variety of structs. You can only use
pointers on a very restricted set of types in C#. But as long as it
works, it's fine to use ToRaw2.

If performance is your top priority then I guess ToRaw2 is the better
alternative. But I would also consider a third alternative that used
the BitConverter class to convert each member separately. The benefit
there is that the code doesn't require full trust to run.
Ah, very good. Just making sure that I wasn't missing anything
obvious. I'm trying to run as close to real-time as possible. Thank
you for the prompt reply.

Feb 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tien Pham via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Tien Pham I am having problems with intptr. Or maybe i am not using it properly. What it is that i am having problem with is...
7
by: Prabhu | last post by:
Hi, I have to send a structure through TCPClient socket. we can send only byte array through the socket, So please any one can help me by telling How to convert a struct object into an byte...
16
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
1
by: Me | last post by:
I'm trying to get a structure into a byte array. I can't seem to figure out how to get a non-fixed length null-terminated string into the array (without rolling my own logic). For example, a...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
3
by: tash.robinson | last post by:
I have been pulling my hair out with this and have searched and found some solutions but I can't seem to get them to work as expected. I have a structure, which I have declared explicitly using...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
6
by: carles | last post by:
Hi, Here, sample code where a byte array is used to fill a particular structure: fs = File.OpenRead(path); // FileStream BITMAPFILEHEADER bfh = new BITMAPFILEHEADER(); b = new byte;
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.