sizeof () estructuras desconocidas. ¿Por qué?

Why can't I use sizeof() on simple structs?

p.ej:

private struct FloatShortPair
{
    public float myFloat;
    public short myShort;
};

int size = sizeof(FloatShortPair);  //CS0233

error CS0233: 'FloatShortPair' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)

MSDN establece lo siguiente:

The sizeof operator can only be used for types that are compile-time constants. If you are getting this error, make sure that the size of the identifier can be determined at compile time. If it cannot, then use SizeOf instead of sizeof.

How are float and short not compile time constants? 8-/

preguntado el 08 de noviembre de 11 a las 09:11

1 Respuestas

Los tamaños de short y float are constant - but how the CLR decided to pack that float in memory no es necessarily constant. For example, on a 64-bit processor it may decide to align each value on an 8-byte boundary.

De la especificación C # 4, sección 18.5.8:

For certain predefined types, the sizeof operator yields a constant value as shown in the table below.

[...]

For all other types, the result of the sizeof operator is implementation-defined and is classified as a value, not a constant.

[...]

For alignment purposes, there may be unnamed padding at the beginning of a struct, within a struct, and at the end of a struct.

Tenga en cuenta que enlatado utilizado sizeof in this situation, within an unsafe context. Whether you should use that or Marshal.SizeOf depende de lo que intente hacer.

respondido 08 nov., 11:14

As the error message suggests use System.Runtime.InteropServices.Marshal.SizeOf. - Mongus Pong

Related question (sorry to hijack): If you have finer control over the layout of the type (StructLayoutAttribute with explicit offsets) does that make it a compile time constant? - MattDavey

@MattDavey: Not according to the spec. Basically the C# language specification tries not to get into the implementation details of things like StructLayoutAttribute. - Jon Skeet

If one wanted to optimize an IList<T> implementation for scenarios where many moderate-sized lists would be created, used briefly, and abandoned, keeping all arrays small enough to avoid LOH allocations would be a major win. By what means should a class choose the maximum inner array size if it can't find out the actual amount of space .NET will require for each element? - Super gato

I know that the process for deciding what objects should go on the LOH could change, but keeping arrays below 85,000 bytes would improve performance at least on today's version of .NET; even if some other strategy might be better in a future .NET, code which keeps allocations below 85,000 bytes should continue to at least work decently. But to make code keep allocations below 85,000 bytes with today's .NET, it would have to know their size with today's .NET. - Super gato

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.