DCSIMG
Quiz #2 - Ido Samuelson's blog

Quiz #2

What is the difference between this structures if there is a difference at all?
 

        [StructLayout(LayoutKind.Sequential)]

        struct SampleStructA

        {

            byte b;

            int i;

            byte b2;

        }

 

        [StructLayout(LayoutKind.Explicit)]

        struct SampleStructB

        {

            [FieldOffset(0)]

            byte b;

            [FieldOffset(1)]

            int i;

            [FieldOffset(5)]

            byte b2;

        }

 

Published Tuesday, July 25, 2006 8:26 AM by Ido Samuelson

Comments

# re: Quiz #2

Thats pretty obvious.
Sequential layout put the struct data in seuential order, that is, byte, int byte.
The Explicit layout lets you specify the order.
The fact that you put offset 5 on b2 means that b2 points the last byte inside i.

If you specify all the offsets to 0 you get the same functionality as C++ unions...

Wednesday, July 26, 2006 12:57 PM by Eran Kampf

# re: Quiz #2

Eran, I think you have a mistake. The offset states that the int will get 4 bytes (from 1 to 4) and b2 will be located in byte 5.
I think that both are equal if the int is 32 bit => 4 bytes. If running on 64 machine, int might be 8 bytes and than there will be a difference.

Wednesday, July 26, 2006 5:23 PM by Elad Winder

# re: Quiz #2

I am not talking about 64bit.
Eran, you have a small mistake there. Keep trying :)

Wednesday, July 26, 2006 5:37 PM by Ido Samuelson

# re: Quiz #2

Oh yeah I got confused.
Ok, the two structures seem to be the same but the second one (the Explicit) is definitely contiguous because you manually defined the offset to behave this way.
The first structure (Squential) might be noncontiguous, depending on the  StructLayoutAttribute.Pack value which controls the alignment of the fields in the structure.
Its not clear from the docs but as I understand it, if Pack value is 1 than both structure will be identical, otherwise, gaps will occur between fields...

Thursday, July 27, 2006 12:00 AM by Eran Kampf

# re: Quiz #2

Pack on a 32bit is 4 bytes. This is a compiler option to increase performance in accessing the data.

SampleStructA takes 12 bytes in memory while SampleStructB takes 8 bytes.

Another question to follow this is when we will use Pack=1 option?

Good Job!

Thursday, July 27, 2006 12:42 AM by Ido Samuelson

# re: Quiz #2

When you're only using bytes?

Thursday, July 27, 2006 2:50 PM by Eran Kampf

# re: Quiz #2

If your struct only contain bytes and you are using pack=1 you reduce the performance. However sometimes you will need this reduce of performance. what can be this scenario?

Friday, July 28, 2006 6:24 AM by Ido Samuelson

# re: Quiz #2

Beats me...
Maybe on environments where you have limited memory space and saving memory is more important than performance, like cellphones etc.?

Friday, July 28, 2006 11:14 AM by Eran Kampf

# re: Quiz #2

Half answer :) but good one!
The other part to go with pack=1 is whenever you cross machine boundaries. This is in order to reduce network bandwidth thus increase your overall performance in a multi machine scenario.

Friday, July 28, 2006 4:02 PM by Ido Samuelson

# re: Quiz #2

Dam... finally i find something i already ran into before, and you answered it already... ;)

Tuesday, August 15, 2006 4:11 PM by Roy Elimelech

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: