11

Is it 12 bytes or 16 bytes when stored in a List<DataPoint>?

public struct DataPoint
{
    DateTime time_utc;
    float value;
}

Is there any sizeof function in C#?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Meh
  • 7,016
  • 10
  • 53
  • 76
  • I hope you are aware that each instance of this struct will be boxed when put in a list, so add the space for the pointer. –  Sep 27 '10 at 14:19
  • 9
    @ToxicAvenger: it will *not* be boxed when you put it inside a `List`. It would be boxed in an `ArrayList` or `List` though. – Ruben Sep 27 '10 at 14:22
  • This is more specific, but possibly a duplicate of http://stackoverflow.com/questions/3361986/how-to-check-the-number-of-bytes-consumed-by-my-strucure – Philip Rieck Sep 27 '10 at 14:26

6 Answers6

14

Take a look at @Hans Passant's answer here for interesting background on this issue, esp. with regard to the limitations of Marshal.Sizeof.

Community
  • 1
  • 1
Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
8

The CLR is free to lay out types in memory as it sees fit. So it's not possible to directly give "the" size.

However, for structures it's possible to restrict the freedom of the CLR using the StructLayout Attribute:

  • Auto: The runtime automatically chooses an appropriate layout.
  • Sequential: The members of the object are laid out sequentially and are aligned according to the StructLayoutAttribute.Pack value.
  • Explicit: The precise position of each member is explicitly controlled.

The C# compiler automatically applies the Sequential layout kind to any struct. The Pack value defaults to 4 or 8 on x86 or x64 machines respectively. So the size of your struct is 8+4=12 (both x86 and x64).


Unrelated from how a type is laid out in memory, it's also possible to marshal a type in .NET using the Marshal Class. The marshaller applies several transformations when marshalling a type, so the result is not always the same as the way the CLR laid out the type. (For example, a bool takes 1 byte in memory plus alignment, while the marshaller marshals a bool to 4 bytes.)

dtb
  • 213,145
  • 36
  • 401
  • 431
  • 1
    Note it's possible with the IL `sizeof` instruction to get the "final" CLR size of a struct, taking into account padding and any other alignment operations. See [my blog post](http://lastyearswishes.com/blog/view/50a30d3bd1f1a5234323b198) for how to use it and more differences (note: I wrote a wrapper library that can be called from C#) – Earlz Nov 14 '12 at 04:06
7

Marshal.SizeOf()

http://msdn.microsoft.com/en-us/library/y3ybkfb3.aspx

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
4

It will be 12 bytes (4 for float, 8 for DateTime); Marshal.SizeOf will return 16 because the default packing is 8 bytes aligned. This is a good article on structs and packing. It gives a full description of whats actually happening.

SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
0

Try Marshal.SizeOf(typeof(DataPoint))

elsni
  • 1,953
  • 2
  • 17
  • 35
0

The following code is based on this StackOverflow question and answers:

        /// <summary>
        /// Computes the size of the given managed type. Slow, but reliable. Does not give the same result as Marshal.SizeOf
        /// NOTE: this is not the same as what is the distance between these types in an array. That varies depending on alignment.
        /// </summary>
        public static int ComputeSizeOf(Type t)
        {
            // all this just to invoke one opcode with no arguments!
            var method = new DynamicMethod("ComputeSizeOfImpl", typeof(int), Type.EmptyTypes, typeof(Util), false);
            var gen = method.GetILGenerator();
            gen.Emit(OpCodes.Sizeof, t);
            gen.Emit(OpCodes.Ret);
            var func = (Func<int>)method.CreateDelegate(typeof(Func<int>));
            return func();
        }

I think the question you are probably wondering, is not what is the size of the type but what is the distance between two contiguous elements in the List. This is because alignment can play a role, as mentioned by others.

I believe the solution to that problem would best be achieved using Marshal.UnsafeAddrOfPinnedArrayElement(), but is very difficult to use correctly, particularly because the List does not expose publicly the backing array.

cdiggins
  • 17,602
  • 7
  • 105
  • 102