question about C++0X memory location

K

kuangye

According to C++0x, a memory location is either an object of scalar
type, or a maximal sequence of adjacent bit-fields all having non-zero
width.

Then, I have a question about this:

struct A{
int b:5, c:11, d:16;
};

How many memory locations are there in struct A.
One or Two? And why ?
 
K

kuangye

I think there a one memory location, according to "a maximal sequence
of adjacent bit-fields all having non-zero width".

But two is reasonable too. Since A::d is align to 2.
 
A

Anthony Williams

kuangye said:
According to C++0x, a memory location is either an object of scalar
type, or a maximal sequence of adjacent bit-fields all having non-zero
width.

Then, I have a question about this:

struct A{
int b:5, c:11, d:16;
};

How many memory locations are there in struct A.
I think there a one memory location, according to "a maximal sequence
of adjacent bit-fields all having non-zero width".

But two is reasonable too. Since A::d is align to 2.

It is one memory location. This ensures maximum portability to platforms
with different-sized machine words (e.g. on a 32-bit machine this is all
one word). The only consequence is that accessing the individual bit
fields cannot be done from different threads without synchronization,
otherwise you get a data race and undefined behaviour. If you wish them
to be separate memory locations, add a zero-length bit field:

struct B{
int b:5, c:11, dummy:0,d:16;
};

This may force the compiler to emit padding to move d to a separate
word.

Anthony
 
K

kuangye

Thanks.

I have another question about this, that is what is the meaning of
the scalar type?
Does it mean the any c++ primitive types(eg:char, short int, int, long
long, ..) or just the type which size is equal to the platform width.

#pragma push pack(1) //without padding between members
struct B{
char c1;
char c2;
int i1;
} b;
#pragma pop pack()

Can I update b.c1, b.c2 and b.i1 in different threads without
synchronous them ?
 
A

Anthony Williams

kuangye said:
I have another question about this, that is what is the meaning of
the scalar type?
Does it mean the any c++ primitive types(eg:char, short int, int, long
long, ..) or just the type which size is equal to the platform width.

Scalar types are defined in 3.9p9 of the FCD
(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf):

"Arithmetic types (3.9.1), enumeration types, pointer types, pointer to
member types (3.9.2), std::nullptr_t, and cv-qualiï¬ed versions of these
types (3.9.3) are collectively called scalar types."
#pragma push pack(1) //without padding between members
struct B{
char c1;
char c2;
int i1;
} b;
#pragma pop pack()

Can I update b.c1, b.c2 and b.i1 in different threads without
synchronous them ?

Yes.

Anthony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top