Mark Ingram
Mark Ingram

Reputation: 73693

Struct member initialisation

Whilst reading through the DirectWrite source code I came across the following struct:

/// <summary>
/// Line breakpoint characteristics of a character.
/// </summary>
struct DWRITE_LINE_BREAKPOINT
{
    /// <summary>
    /// Breaking condition before the character.
    /// </summary>
    UINT8 breakConditionBefore  : 2;

    /// <summary>
    /// Breaking condition after the character.
    /// </summary>
    UINT8 breakConditionAfter   : 2;

    /// <summary>
    /// The character is some form of whitespace, which may be meaningful
    /// for justification.
    /// </summary>
    UINT8 isWhitespace          : 1;

    /// <summary>
    /// The character is a soft hyphen, often used to indicate hyphenation
    /// points inside words.
    /// </summary>
    UINT8 isSoftHyphen          : 1;

    UINT8 padding               : 2;
};

Notice the strange " : " after each member declaration. I'm going to assume it's a default initialisation value for the member variable.

I tried searching Google to confirm, but without knowing exactly what it's called I didn't get far (most of the results where to do with default initialisation).

What is the name of this technique?

Upvotes: 2

Views: 334

Answers (4)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361712

Notice the strange " : " after each member declaration. I'm going to assume it's a default initialisation value for the member variable.

It's not default initialization. It means breakConditionBefore is just 2 bit integer, isWhitespace is a 1 bit integer. And so on.

In DWRITE_LINE_BREAKPOINT, one 8-bit integer (i.e UINT8) is divided amongst 5 members, 3 of which are 2-bit integers, and 2 members are 1-bit integers.

Read about Bit-fields

Upvotes: 5

liaK
liaK

Reputation: 11648

Well, they are Bit fields.

The Standard docs themselves has an example for you.

From 1.7.5 The C++ Memory Model,

[ Example: A structure declared as

struct {
char a;
int b:5,
c:11,
:0,
d:8;
struct {int ee:8;} e;`
}

contains four separate memory locations: The field a and bit-fields dand e.ee are each separate memory locations, and can be modified concurrently without interfering with each other. The bit-fields b and c together constitute the fourth memory location. The bit-fields b and c cannot be concurrently modified, butb and a, for example, can be. —end example ]

Upvotes: 1

Abhijit
Abhijit

Reputation: 31

No, its not default initialization list but bit field. Please refer http://en.wikipedia.org/wiki/Bit_field.

Upvotes: 3

Fred Foo
Fred Foo

Reputation: 363807

The :2 declares a member of 2 bits. This is called a bitfield. Since the total number of bits declared add up to 8, all the bitfield members are adjacent and their type is UINT8, a struct DWRITE_LINE_BREAKPOINT is a single byte in size.

Upvotes: 2

Related Questions