1

From the November 2014 working draft of the C++14 standard:

§ 1.7 6

c ISO/IEC N4296 5 [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 d and 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, but b and a, for example, can be. — end example ]

I assume that the :0 acts as a separator of sorts, which is why d has a separate memory location while b and c do not. However, I do not understand what is meant by

together constitute the fourth memory location

Are b and c a union? E.g., equivalent to

union {
    int b:5;
    int c:11;
};
  • possible duplicate of [Simple C syntax Question](http://stackoverflow.com/questions/7370792/simple-c-syntax-question). Bitfields 'are' not a `union`, they are a bitfield. – Jongware May 19 '15 at 22:50
  • @Jongware: That question explains what bit-fields are. That is not at all my question. –  May 19 '15 at 22:51
  • But it explains what bitfields are. And they are not a `union` according to the explanation. – Jongware May 19 '15 at 22:54
  • @Jongware: It explains what a bit-field is. It does not explain what is meant by two separate bit-fields constituting a single memory location, which is what I am asking about. –  May 19 '15 at 22:55
  • Does this question about [C++ memory model](http://stackoverflow.com/questions/6319146/c11-introduced-a-standardized-memory-model-what-does-it-mean-and-how-is-it-g?rq=1) help at all? – Guvante May 19 '15 at 22:56
  • @Guvante: That one definitely helps. Thank you –  May 19 '15 at 22:58

1 Answers1

1

No, they do not share any bit as they would in the union example.

They just are considered one unit for the purposes of considering memory locations.

Put another way, the following would be the bits in your example (potentially)

AAAAAAAA BBBBBCCCCCCCCCCC DDDDDDDD EEEEEEEE (ee sharing e)

The spaces are to show the memory locations and don't take up any space in memory.

Guvante
  • 18,775
  • 1
  • 33
  • 64
  • @R_Kapp: A memory location may contain more than one bit field. – Thomas Matthews May 19 '15 at 22:52
  • What purpose does that serve, then? –  May 19 '15 at 22:52
  • @R_Kapp: You need to read later on when they talk about memory locations and what they mean. Remember that C++ is attempting to abstract over a physical machine and make certain guarantees. Memory locations are involved in defining those guarantees. Basically it revolves around the compromise that most systems only have byte level access so trying to make bit level guarantees is impossible. – Guvante May 19 '15 at 22:54
  • The purpose is to pack or make better use of memory. For example an 8-bit byte could hold up to 8 bit fields in that one byte. Otherwise, one could use 1 bit per byte and occupy 8 bytes. That may be a lot of room on some platforms. – Thomas Matthews May 19 '15 at 22:54