Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does accessing array of POD struct as array of its single member violate strict aliasing?

Tags:

I have integer values that are used to access data in unrelated data stores, i.e., handles. I have chosen to wrap the integers in a struct in order to have strongly typed objects so that the different integers cannot be mixed up. They are, and must be, POD. This is what I am using:

struct Mesh {
    int handle;
};
struct Texture {
    int handle;
};

I have arrays of these handles, such as: Texture* textureHandles;.

Sometimes I need to pass an array of handles as int* to more generic parts of the code. Right now I'm using:

int* handles = &textureHandles->handle;

which essentially takes a pointer to the first element of the struct and interprets it as an array.

My question is basically if this is legal, or if it violates strict aliasing to manipulate int* handles and Texture* textureHandles pointing to the same memory. I think this should be allowed since the underlying type (int) is accessed the same way in both cases. The reservation I have is related to the fact that I access multiple structs by taking the address of a member inside one struct.

As an extension to my first question, would the following be ok?

int* handles = reinterpret_cast<int*>(textureHandles);