I need to determine the size of the data / contents in a structure and not the size of the structure including the data which can be determined by using sizeof or SizeOf.
example:
struct
{
int data1;
double data2;
}
sizeof or SizeOf returns "16" but the actual size of the data is 12 - how to determine that
Thanks
Ole

How to determine the size of data in a struct?
Asura
Hi Ole
I think that's because of the struct member alignment (default 8byte). Each member in the struct is aligned to the next 8 byte border. That makes a size of 16 bytes for your struct.
This struct takes 16 bytes in the memory, for what purpose do you need the added size of the struct members
Adrian
Btw, at least for c++ projects the struct member alignment can be configured in the project properties.
hypermatt
ohh yes - you're right - it is the alignment that causes the extra bytes. However I need the size of the data to use as a divider in calculating a byte array.
thanks
ole
ahmed Abd-El Atty
You can try and use reflection
public
static unsafe void GetSize(){
Type t = typeof(s) ; //s is the struct FieldInfo[] fs = t.GetFields(); FieldInfo f1 = fs[0]; int size = 0; foreach (FieldInfo f1 in fs){
switch (f1.FieldType){
case(typeof(double)):size += 8;
break; //Add more as necessary..hope this helps}
}
}
andy_vc