0

I'm sorry this is long, but it is a little complicated to explain.

We recently had to hand in for homework the following program (much simplified here):

  • Some type of structure (class/struct) representing a physical block of data (just a char[1024])
  • Two types of logical partitioning of this block

For example:

struct p {
  char[1024]
}
struct l1 {
  int num;
  char name[20];
}
struct l2 {
  int num;
  char type[10];
  char filler[400];
  bool flag;
}

The obvious thing to me was to have a union

union {
  p phy;
  l1 logi1;
  l2 logi2;
}

but the problem was that part of the specification (the part I cut out to simplify it) was that the physical stuff be in a separate file then the logical stuff.

So now the question is: Is there a way to add fields to the union (I assume not) or another way to have functions in the 'physical' file accept 'logical' blocks and use them as raw blocks?

I hope this is clear.

P.S. This was due already and I solved it with reinterpret_cast. I was wondering if there was a more elegant way.

timrau
  • 22,578
  • 4
  • 51
  • 64
Baruch
  • 20,590
  • 28
  • 126
  • 201
  • You cant solve this with `reinterpret_cast` since that would break the strict aliasing rule. – Šimon Tóth Feb 17 '11 at 21:44
  • @Let_Me_Be What is the strict aliasing rule? – Baruch Feb 17 '11 at 21:52
  • @baruch http://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule – Šimon Tóth Feb 17 '11 at 21:53
  • If you read in a block as a physical struct, and use `reinterpret_cast<>` to point a logical structure pointer to it, what can the optimizer do to break this? File operations are observable behavior, and hence can't be optimized out. – David Thornley Feb 17 '11 at 21:58
  • @David, if you read into the physical struct but then make no further reference to it (because you're using another pointer of a different type pointing at the same memory), the compiler could keep the file operation but throw away the buffer afterward. – Rob Kennedy Feb 17 '11 at 22:01
  • 1
    How does the strict aliasing rule affect 5.2.10/7 of the C++ Standard, which specifies that `reinterpret_cast<>` can be used to cast a pointer to a pointer of different type (assuming appropriate type alignments)? This being a C++ question, I'm not interested in the C99 standard, and that's what seems to be primarily referred to in the linked question. – David Thornley Feb 17 '11 at 22:08
  • @David Well, they are unrelated. `reinterpret_cast` is just a cast that will allow you to cast two unrelated types. If that cast does or does not break strict aliasing rule isn't affected. – Šimon Tóth Feb 18 '11 at 00:33

2 Answers2

0

No, the entire structure of a type must be defined together. You cannot "re-open" a type to add things to its definition later.

The assignment as you stated it here asked for three types, not one type representing three things. Your first three struct definitions were sufficient.

After all three structs have been defined, you're welcome to define a union type. Possibly something like this:

#include "physical.h"
#include "logical.h"

union combined_structure {
  p phy;
  l1 logi1;
  l2 logi2;
};
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • I needed to have functions in the physical part accept 'blocks' of data, which were represented as 'logical' blocks. – Baruch Feb 17 '11 at 21:51
0

What you can do is to create two unions. As long as you don't interchange them you will be fine.

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