11

Consider the following struct:

struct Vector4D
{
   union
   {
      double components[4];
      struct { double x, y, z, t; } Endpoint;
   };
};

It seems to me that I have seen something similar in WinApi's IPAddress struct. The idea is to give me the possibility to use the array components both by index and by name, for example:

Vector4D v;
v.components[2] = 3.0;
ASSERT(v.Endpoint.z == 3.0) //let's ignore precision issues for now

In the C++ standard there is a guarantee that there will be no "empty" space at the beginning of a POD-struct, that is, the element x will be situated right in the beginnig of the Endpoint struct. Good so far. But I don't seem to find any guarantees that there will be no empty space or padding, if you will, between x and y, or y and z, etc. I haven't checked out the C99 standard though.

The problem is that if there is an empty space between Endpoint struct elements, then the idea will not work.

Questions:

  1. Am I right that there indeed is no guarantee that this will work either in C or C++.

  2. Will this practically work on any known implementation? In other words, do you know of any implementation where this doesn't work?

  3. Is there any standard(I mean not compiler-specific) way to express the same idea? Maybe the C++0x alignment features might help?

By the way, this isn't something I am doing in production code, don't worry, just curious. Thanks in advance.

timrau
  • 22,578
  • 4
  • 51
  • 64
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434

4 Answers4

5
  1. yes
  2. depends on the alignment needs of the architecture and the compilers strategy
  3. no, but you could make a object wrapper (but you will end up with .z() instead of just .z)

Most compilers should support squashing a structure using a pragma or an attribute. #pragma pack for example.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
4

You can circumvent any memory alignment issues by having references to each element of the array, as long as you declare the array before the references in the class to ensure they point to valid data. Having said that I doubt alignment would be an issue with doubles, but could be for other types (float on 64bit arch perhaps?)

#include <iostream>
using namespace std;

struct Vector4D
{
    Vector4D() : components(), x(components[0]), y(components[1]), z(components[2]), t(components[3]) { }

    double components[4];

    double& x;
    double& y;
    double& z;
    double& t;
};

int main()
{
    Vector4D v;

    v.components[0] = 3.0;
    v.components[1] = 1.0;
    v.components[2] = 4.0;
    v.components[3] = 15.0;

    cout << v.x << endl;
    cout << v.y << endl;
    cout << v.z << endl;
    cout << v.t << endl;
}

Hope this helps.

virgesmith
  • 41
  • 1
  • I thought of this too, but it has a MAJOR drawback. When I take a const Vector4D, the components will be const, but I'll be able to change them through references. This is very very bad – Armen Tsirunyan Jun 23 '11 at 11:29
  • You can always work around this C++ish issue using a smarter proxy that propagate const-ness. – Matthieu M. Jun 23 '11 at 11:35
  • 1
    @Matt, This makes me wonder about reference types that propagate constness, when using a special declaration specifier. Like, for a constant `v`, that `v.x` would have type `double const`. Would such a language feature make sense? – Johannes Schaub - litb Jun 23 '11 at 11:42
  • 1
    @Johannes: I am afraid it would only further bloat the language. Personally I would favor constness propagation by default, but this is the "functional" view of constness, while C++ is ingrained with the "technical" aspect. – Matthieu M. Jun 23 '11 at 16:55
  • 2
    And now the vector structure is twice as large. – Ruslan Aug 29 '17 at 20:47
2

When it comes to the standard, there are two problems with it:

  • It is unspecified what happens when writing to an element in a union and reading from another, see the C standard 6.2.6.1 and K.1
  • The standard does not guarantee the layout of the struct match that of the layout of the array, see the C standard 6.7.2.1.10 for details.

Having said this, in practice this will work on normal compilers. In fact, this kind of code is widely spread and is often used to reinterpret values of one type into values of another type.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
0

Padding bytes will not cause an issue as all variables are of type double. The compiler will treat Vector4D as a double array. That means, v.Endpoint.z is essentially the same as v[2].

Vijay Mathew
  • 26,737
  • 4
  • 62
  • 93
  • Do you mean practically or you also mean that the standard mandates my code to work correctly? – Armen Tsirunyan Jun 23 '11 at 10:35
  • 1
    Are you sure? I don't see a problem with `double` having 6 bytes. – Šimon Tóth Jun 23 '11 at 10:36
  • @Let_Me_Be How will 6 byte `double`s with 2 byte padding in between break this code? – Vijay Mathew Jun 23 '11 at 10:46
  • 2
    @Vijay Any padding will break the code. Arrays don't have padding. – Šimon Tóth Jun 23 '11 at 10:48
  • @Let_Me_Be `Vector4D` is essentially an array of `double`s. `v.Endpoint.z` will be compiled to `v + 2`. – Vijay Mathew Jun 23 '11 at 10:52
  • @Vijay Not if there will be padding. In this case the question is whether a compiler can decide to add padding to all structure members, even thought they don't need it. In case of a 6 byte `double`, that needs to be aligned to 2 bytes (since it can't require 4 byte alignment) would be (unnecessarily) aligned to 4 bytes. I actually don't see a problem with that. And I'm pretty confident that the standard doesn't either. – Šimon Tóth Jun 23 '11 at 10:56
  • @Vijay: I think we all agree that practically it will be so on most platforms. But as far as the C++ standard is concerned, there is no such guarantee. Is there? – Armen Tsirunyan Jun 23 '11 at 10:56
  • @Let_Me_Be Padding need not prevent the compiler from treating the union as an array. (http://en.wikipedia.org/wiki/Stride_of_an_array). I agree that there is no "standards" support for my argument. But this code will work on all common platforms. I am yet to come across an architecture that has 6 byte `double`s! – Vijay Mathew Jun 23 '11 at 11:12