Jump to content
  • Advertisement
Sign in to follow this  
IFooBar

strict aliasing, private inheritance and vectors

This topic is 2500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 (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.

Share this post


Link to post
Share on other sites
Advertisement

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?

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
But how does it do that? How can it represent "emptiness" for any type of vector data?

That's what I was getting at.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!