Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

C++/CLI: Attributes


StructLayout and FieldOffset

The first of these attributes allows the programmer to control the physical layout of the fields in a ref class or value class. There are three possible orderings, Auto, Explicit, and Sequential, each of which is specified by an enumeration value of the same name, from the enumeration type LayoutKind. Auto layout results in the runtime's choosing an appropriate layout; this is the default. Explicit layout allows the programmer to dictate the precise position of each field by using a FieldOffset attribute. With sequential layout, the fields are laid out sequentially, in the order in which they are declared, with some suitable packing between adjacent fields. Here's an example of explicit layout control, and the output it produces:

using namespace System;
using namespace System::Runtime::InteropServices;



[StructLayout(LayoutKind::Explicit)]
ref struct Overlap
{
        [FieldOffset(0)] double d;

        [FieldOffset(0)] long long int lli;

        [FieldOffset(0)] int i0;
        [FieldOffset(4)] int i1;

        [FieldOffset(0)] unsigned char b0;
        [FieldOffset(1)] unsigned char b1;
        [FieldOffset(2)] unsigned char b2;
        [FieldOffset(3)] unsigned char b3;
        [FieldOffset(4)] unsigned char b4;
        [FieldOffset(5)] unsigned char b5;
        [FieldOffset(6)] unsigned char b6;
        [FieldOffset(7)] unsigned char b7;
};



int main()
{
	Overlap o;
	o.lli = o.i0 = o.i1 = 0;
	o.b0 = o.b1 = o.b2 = o.b2 = o.b3 = o.b4 = o.b5 = o.b6 = o.b7 = 0;
	o.d = 123.456E56;

	Console::WriteLine("d:   {0}", o.d);
	Console::WriteLine("lli: {0:X16} ({1})", o.lli, o.lli);
	Console::WriteLine("i:   {0:X8} {1:X8} ({2}, {3})",
					o.i1, o.i0, o.i1, o.i0);
	Console::WriteLine("b:   {0:X2} {1:X2} {2:X2} {3:X2} {4:X2} "
					"{5:X2} {6:X2} {7:X2}",
					o.b7, o.b6, o.b5, o.b4, o.b3, o.b2, o.b1, o.b0);
}

The output produced is:

d:   1.23456E+58
lli: 4BFF77E1401420EC (5476227481232220396)
i:   4BFF77E1 401420EC (1275033569, 1075060972)
b:   4B FF 77 E1 40 14 20 EC

The attribute StructLayout is applied to the type as a whole, while each of the fields that type contains has the attribute FieldOffset. In Visual C++, the field lli exactly overlays the double d, the pair of ints i0 and i1, and the eight byte fields b0b7, resulting in a type having the characteristics of a Standard  C++ union. This is confirmed by the output produced.

Consider the following layouts (see directory At05):

[StructLayout(LayoutKind::Explicit)]
value struct SL1
{
        [FieldOffset(0)] int v;
        [FieldOffset(4)] unsigned char b;
        [FieldOffset(8)] int w;
};

In this example, the layout is explicit, with the three fields having addresses 4 bytes apart.

[StructLayout(LayoutKind::Sequential, Pack = 4)]
value struct SL2
{
        int v;
        unsigned char b;
        int w;
};

In this example, the layout is sequential, there are no explicit offsets, and the packing factor is 4. This also results in the three fields having addresses 4 bytes apart.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.