Sign in to follow this  
Yratelev

c# struct size

Recommended Posts

Yratelev    130
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

Share this post


Link to post
Share on other sites
Programmer16    2321
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!

Share this post


Link to post
Share on other sites
Washu    7829
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.

Share this post


Link to post
Share on other sites
intrest86    742

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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
turnpast    1011
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.

Share this post


Link to post
Share on other sites
Yratelev    130
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]

Share this post


Link to post
Share on other sites
turnpast    1011
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this