strict aliasing, private inheritance and vectors

Started by
4 comments, last by ApochPiQ 12 years, 8 months ago
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 (link). 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); }
};
[/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.
[size=2]aliak.net
Advertisement
If you're not familiar with the strict aliasing rule, this has some good info.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


If you're not familiar with the strict aliasing rule, this has some good info.


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?
[size=2]aliak.net
Depends on what shared_null does, really.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.
[size=2]aliak.net
But how does it do that? How can it represent "emptiness" for any type of vector data?

That's what I was getting at.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement