3

I'm building a app and I need to handle a position in a graph. This position have value of X, Y and the direction like north, east, south and weast. I think this position could be a Struct because its represent a single value in that graph.

I researched and thought about Structs and find these rules to use a Struct:

  • It logically represents a single value, similar to primitive types (integer, double, and so on).
  • It has an instance size smaller than 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.

Here's an example of my unfinished Struct:

public struct Position
{
   public long PositionX { get; set; }

   public long PositionY { get; set; }

   public CompassPoint CompassPoint { get; set; }
}

public enum CompassPoint : byte
{
   North,
   East,
   South,
   West
}

I want to know how to calculate the size in bytes of my Struct and how I'll know if it is immutable?

Thanks.

Update:

Okay. According to the responses it seems that my Struct passed the 16 bytes because only two long have 16 bytes + 1 byte of CompassPoint.

But an extra question would be: What I gained have using a Struct with 16 bytes and immutable? And look at DateTime Struct, it seems have more thatn 16 bytes? Any problem?

Acaz Souza
  • 8,311
  • 11
  • 54
  • 97
  • Isn't each long 8 bytes, sounds like you are going to have more than 16 bytes here if you really have two longs and something else. – Hogan Aug 29 '12 at 00:48
  • 1
    It's mutable, because you have public `set` accessors. Immutable means it's not possible to change properties after creation. – Blorgbeard Aug 29 '12 at 00:50
  • 1
    The `DateTime` struct is only 8 bytes. The `Kind` and `Ticks` properties is the only actual data in it, and they are stored togethering in a `long`. Regarding the 16 byte recommendation: http://stackoverflow.com/questions/2437925/why-is-struct-better-with-being-less-than-16-bytes/2437938#2437938 – Guffa Aug 29 '12 at 01:10
  • 1
    With a <= 16 byte immutable struct, you gain "value semantics", and better performance in some cases. Maybe see: http://stackoverflow.com/questions/2299606/ – Blorgbeard Aug 29 '12 at 01:20

2 Answers2

6

The size depends on what the CompassPoint type is. However, each long will use 8 bytes, so you are already over the recommended maximum of 16 bytes. It's not a hard rule though, but you may get worse performance than if it was a class, depending on how you use it.

The exact size of the struct is determined by the JIT compiler, but genreally you can just add the sizes of the members to predict the size. Small data types will be padded though, so if your CompassPoint uses just a single byte, it will still require 4 or 8 bytes to make the struct size reach an even boundary.

The struct is not immutable, as you can change the properties in it. Make the setters private, and add a constructor that can create it to make it immutable:

public struct Position {

  public long PositionX { get; private set; }
  public long PositionY { get; private set; }
  public CompassPoint CompassPoint { get; private set; }

  public Position(long x, long y, CompassPoint compass) {
    PositionX = x;
    PositionY = y;
    CompassPoint = compass;
  }

}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • @AcazSouza: The enum will use one byte, so the struct comes to 20 bytes in total with padding. Possibly 24 bytes on an x64 system. – Guffa Aug 29 '12 at 00:56
  • Thanks for response but I have some more question about that. You can help me? See the update please. Another question is why 20? 2 long = 16 + 1 byte = 17 o.O. Why 20? Unmanaged bytes? – Acaz Souza Aug 29 '12 at 01:03
  • @AcazSouza: Structs are padded to an even word boundary to make memory access easier. If you would store the struct on an odd address, the CPU would need to read the words that the struct spans, and shift the data. – Guffa Aug 29 '12 at 01:09
2

You could use the Marshal.SizeOf method to get the unmanaged size, in bytes, of your struct:

Position pos = new Position(); // this constructor call is not necessary for structs
int sizeInBytes = Marshal.SizeOf(pos);

But be aware that the unmanaged and managed sizes of an object can differ sometimes.

Thomas C. G. de Vilhena
  • 13,819
  • 3
  • 50
  • 44