3

A neat feature in C low-level device/network programming is that you can control how a struct is aligned in memory. So if I know a hardware device will send me binary data in a given format, I can simply copy that raw data into a struct and then access the members, rather than write de-serialization code to populate a POD object by reading each attribute from the received data buffer.

For instance I might receive an 8-byte payload:

  • BYTE0: ID (8bit int)
  • BYTE1-2: COUNT (16 bit int)
  • BYTE3-6: SENSOR-VAL (32 bit float)
  • BYTE7: CHECKSUM (8 bit int)

in C I could define my struct to exactly align its members in memory to this (by default they would be padded for performance reasons) - note this is an example not tested code - so I can copy 8 bytes from an arbitrary byte-buffer into the address of a Msg object:

#pragma pack(1)
struct Msg {
   char ID;
   short count;
   float sensorVal;
   char chk;
};

. In the more abstracted world of C#/.NET, is this possible? If not, is the only solution to manually write low-level deserialization code or can I automate that somehow to save the amount of error-prone code being written?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • Use a transfer format. Such as protobuf, JSON or XML. They also have nice ready libraries for most languages so that you don't need to write "error-prone code". – fredrik Sep 24 '19 at 16:47
  • @fredrik when you're talking to a piece of hardware, you must use whatever protocol it supports. Sometimes that is raw TCP/IP, etc. – Mr. Boy Sep 24 '19 at 16:52
  • 3
    Have you tried anything? Looking at other questions with similar tags I see references to [`StructLayout`](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.structlayoutattribute?view=netframework-4.8) and [`FieldOffset`](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.fieldoffsetattribute?view=netframework-4.8).... – Heretic Monkey Sep 24 '19 at 16:53
  • https://kalapos.net/Blog/ShowPost/DotNetConceptOfTheWeek13_DotNetMemoryLayout – Thomas C. G. de Vilhena Sep 24 '19 at 16:53
  • That may be true, but your question does not mention any constraints... "a device" can be anything - including another computer with a program you control. – fredrik Sep 24 '19 at 16:54
  • 1
    It seems odd but C# is *better* controlling the exact struct layout than C – harold Sep 24 '19 at 16:58
  • @HereticMonkey thanks for the tip. I'd never heard of those. If this is a dupe, please tag it. – Mr. Boy Sep 24 '19 at 17:00
  • I don't think it's a dupe... Other questions seem to be about specifics of using those attributes, not about their presence. Frankly I don't know enough about the subject to tell; I was just curious about the question so I did some research... – Heretic Monkey Sep 24 '19 at 17:03
  • @hans seems confident and it looks close, so I'm happy (except that duping to a question without an accepted answer is always a bit despiriting) – Mr. Boy Sep 24 '19 at 17:11
  • 1
    all you need is define `[StructLayout(LayoutKind.Sequential, Pack = 1)]` on your struct and you are good to go ;) – whosrdaddy Sep 24 '19 at 17:26
  • @harold: The language the current maintainers of the C Standard want to describe is fundamentally different from the ones the Standards Committee was chartered to describe, and throws out what the original Committee recognized as being one of the great strengths of C. – supercat Dec 21 '19 at 18:38

0 Answers0