c# struct size

Started by
6 comments, last by turnpast 18 years, 9 months ago
Hi, Just a quick Q, I know classes have more variables than you declare, ie, class TheInt { int IamTheInt; } doesnt just have the same size as an int. However, if it were struct TheInt { int IamTheInt; } would the struct version be 4 bytes in size, ie the same as the sum of the sizes of its fields (in this case a 4 byte integer) Thanks Yratelev
Yratelev
Advertisement
I'm not quite sure about C#
class cTheInt{int IamTheInt;}struct sTheInt{int IamTheInt;}int iSizeOfcTheInt = sizeof(cTheInt);int iSizeOfsTheInt = sizeof(sTheInt);

But in C++ iSizeOfcTheInt and iSizeOfsTheInt are both 4.

Hope that helps!
Why does it matter? You shouldn't really need such information really. Marshal.SizeOf will give you a number, as to if it reflects the actual size of the structure, doubtful.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


In C++ structs are NOT definately the size of their members summed because of practice known as padding. The compiler adds empty memory into certai parts of the struct to make it more efficent to use as far as I understand it). If using memcpy has worked for you like that so far, you have been lucky.

Why do you want to find the size of the stuct in C#? Generally, there is probably a better way of doing whatever it is you need it for.
Turring Machines are better than C++ any day ^_~
yes i would have mentioned that (padding), but it is a little too low level for most. my situation is strictly when every data type is equivalent. if, on the other hand, you have mixed data types, especially structs as member variables, then all hell breaks loose. my bad.
You may want to take a look at the StructLayout attribute as a way to control the size and order of your structs. Though it is typically only useful with interop and unmanaged stuff. It will also alow you to create unions and do some other interesting tricks.

For Example :

[StructLayout(LayoutKind.Explicit, Size=4)]
public struct A {
[FieldOffset(0)]int i;
}

will allow you to do:

int i = 5;
unsafe{
A* a;
a = (A*)&i
}

I believe that the default layout kind in c# is Sequential so the above example may work without the explicit layout.
I want to make a struct Vertex out of struct Vector and struct Colour, and then stick it into DirectX, instead of struct Vertex with float x float y float z etc, since i already have operations defined on vectors and colours.

However if there is substantial extra fields also included in a struct that are hidden to me, i will list the floats x y z r g b and redefine the operations.

Quote:Original post by turnpast
[StructLayout(LayoutKind.Explicit, Size=4)]

Thanks, I think thats the kind of thing im looking for, as DirectX needs the fields in a specific order anyway.

Yratelev

[Edited by - Yratelev on July 19, 2005 4:42:36 AM]
Yratelev
Quote:Original post by Yratelev
I want to make a struct Vertex out of struct Vector and struct Colour, and then stick it into DirectX, instead of struct Vertex with float x float y float z etc, since i already have operations defined on vectors and colours.


This is just fine to do. I do a similar thing myself:
[StructLayout(LayoutKind.Explicit, Size=32)]public struct PNTVert {	[FieldOffset(0)]private Vector3 position;	[FieldOffset(12)]private Vector3 normal;	[FieldOffset(24)]private Vector2 texcoord;	public PNTVert(Vector3 position,Vector3 normal,Vector2 texcoord){		this.position = position;		this.normal = normal;		this.texcoord = texcoord;	}	public Vector3 Position{get{return position;}set{position = value;}}	public Vector3 Normal{get{return normal;}set{normal = value;}}	public Vector2 Texcoord{get{return texcoord;}set{texcoord = value;}}}


Be careful with color though. The supplied ColorValue struct has a size of 16 (4 floats) where DX typically expects a single integer for color.

This topic is closed to new replies.

Advertisement