2

I am using a c# struct as a pseudo-union (by using the LayoutKind.Explicit attribute), to pass network messages around my program. I understand how to use the layout with the primitive types, as they are of a know size.

However, how would I do this with one of the fields being a char array? I know a char is 2 bytes of data (when in unicode format), but how big is char[]? Am I correct in believing that this is a reference type, so its size is not just number of items * 2?

How would I layout the struct for this? Is it even possible?

Venatu
  • 1,264
  • 1
  • 13
  • 24

2 Answers2

4

The size is the width of a reference; so 4 bytes on x86 or 8 bytes on x64. The size of the array is irrelevant, as the array is stored separately on the heap. If you want to serialize that data to a byte stream, then it probably depends on which encoding you use for the char data. UTF16 would indeed be 2 * number of characters, but UTF8 or UTF32 will be different.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thank you very much, that confirmed my thinking. The reference size depends on the compiled setting, not the system it runs on, correct? So a 32 bit assembly has a 4 byte reference size, even when run on a 64 bit os? – Venatu Jul 29 '11 at 21:57
  • @Venatu runtime, not compile-time. So it is OS dependent. You CDN target specific CPU to help pick the runtime (if possible), though – Marc Gravell Jul 29 '11 at 22:51
0

That is strange, shouldn't it be equal to the length times the number of bytes per character ?