Sign in to follow this  
IFooBar

strict aliasing, private inheritance and vectors

Recommended Posts

Hey there,

I'm going through some open source vector code (the Qt framework actually). And they have their own way of storing data in a shared vector [url="https://qt.gitorious.org/qt/qt/blobs/master/src/corelib/tools/qvector.h"](link[/url]). On line 95 and 96 there's a comment about strict aliasing which I am trying to understand. Here's a stripped down version:

[source lang='cpp']
[color="#808000"]struct QVectorData
{
QBasicAtomicInt ref;
int alloc;
int size;
uint sharable : 1;
uint capacity : 1;
uint reserved : 30;

static QVectorData shared_null;

static QVectorData *malloc(int sizeofTypedData, int size, int sizeofT, QVectorData *init);
static QVectorData *allocate(int size, int alignment);
static QVectorData *reallocate(QVectorData *old, int newsize, int oldsize, int alignment);
static void free(QVectorData *data, int alignment);
static int grow(int sizeofTypedData, int size, int sizeofT, bool excessive);
};

template <typename T>
struct QVectorTypedData : private QVectorData
{ // private inheritance as we must not access QVectorData member thought QVectorTypedData
// as this would break strict aliasing rules. (in the case of shared_null)
T array[1];

static inline void free(QVectorTypedData<T> *x, int alignment) { QVectorData::free(static_cast<QVectorData *>(x), alignment); }
};
[/color][/source]

They use private inheritance to get the QVectorData in to the QTypedVectorData structure and the comment says that accessing the base class's members would break aliasing in the case of shared_null (which is just a static object stored somewhere in memory as far as I'm aware - it's used as a shared null vector that each vector's data section is pointed to upon creation).

Basically, how will accessing the base class's members break aliasing with a static object in the base class?

Thanks in advance.

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1313487047' post='4849773']
If you're not familiar with the strict aliasing rule, [url="http://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule"]this[/url] has some good info.
[/quote]

That does indeed have some good info. But I am familiar with strict aliasing and, is not a derived class the dynamic type of the base class and a pointer to a base class a similar type to the pointer of a derived class? In which case the compiler should not enforce strict aliasing? So the comment in the code is still confusing. Seems more like a possible padding issue with accessing base members of QVectorTypedData than an aliasing issue no?

Share this post


Link to post
Share on other sites
It is just used as a "shared" null vector. When a vector is set to null or an empty vector created, it is pointed to the shared null. This shared null just acts like a global empty data section of a vector.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this