sizeof vs Marshal.SizeOf !!!

There are two facilities in C# to determine the size of a type – sizeof operator and Marshal.SizeOf method. Let me discuss what they offer and how they differ. Pardon me if I happen to ramble a bit.

Before we settle the difference between sizeof and Marshal.SizeOf, let us discuss why would we want to compute the size of a variable or type. Other than academic, one typical reason to know the size of a type (in a production code) would be allocate memory for an array of items; typically done while using malloc. Unlike in C++ (or unmanaged world), computing the size of a type definitely has no such use in C# (managed world). Within the managed application, size does not matter; since there are types provided by the CLR for creating\managing fixed size and variable size (typed) arrays. And as per MSDN, the size cannot be computed accurately. Does that mean we don’t need to compute the size of a type at all when working in the CLR world? Obviously no, else I would not be writing this post.

Value Types: User-defined value (and reference types) are composed of the primitive value types exposed by the compiler, most of which exist as keywords – int, bool, char, long, double etc. Since the primitive value types are exposed by the compiler, their sizes are (pre-)defined by the compiler (based on the platform on which the CLR runs). The compiler allows querying the sizes of the primitive value types the sizeof operator. The sizeof operator returns the size of the type in bytes as allocated by the CLR (on the current platform). Refer MSDN for the sizes of primitive types.

However, the sizeof cannot be freely used with user-defined value types (struct) but only if the following conditions are true:-

  • The size of the struct is requested from within an unsafe block.
  • The struct does not contain a reference type as its member.
    Since the size of a reference type cannot be computed (see Reference Types below), the size of the struct cannot be computed too.
  • The struct is not a generic value type.
    sizeof instruction to computer the size, instead of hard coding the size (as is done for primitive types).

Besides, the subtle and bitter thing is that the size depends on other factors such as the pack size used ( StructLayout.Pack ) or character set ( StructLayout.CharSet ) applied on the type definition or the fixed size specified (StructLayout.Size). Unlike in C++, sizeof accepts only a (closed) type known at compile time and not variables.

Reference Types: The sizeof works only for POD types.

Given all this uncertainty in computing the size of a type (using sizeof), will there ever be a need then? In a broader sense, there is one situation. That is when the data is passed out of the managed application – Interop or custom serialization and such. For example, the managed application might want to allocate unmanaged memory for creating\filling a data structure for calling a native API, which takes the data structure as its input or would be populating it with output.

Let us enter the second half (or the better half) – sizeof, which returns the size of the structure depending on the StructLayoutAttribute applied.

Can sizeof Otherwise, it can be used with primitive or user-defined value types. It is allowed to be used with reference types only if the type layout is specified to be LayoutKind.Sequential or LayoutKind.Explicit; else the same exception above will be thrown at runtime.

It is possible that the size returned by sizeof and Marshal.SizeOf are different, as with the case of char. sizeof(char) is 2 since CLR is an Unicode beast. Marshal.SizeOf(char) will return 1 since a char in the unmanaged world takes up one byte. However,Marshal.SizeOf(SomeStruct) may report to that its char member consumes two bytes (by default) or made to take up one byte (if the StructLayout.CharSet=CharSet.Ansi).

Do I need to conclude something?

Show Comments