PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Overlapping class members

Here's the deal - I have two one 16bit member variable, and two 8bit member variables within a C++ class.

Rather than each one occupy it's own position in memory, I'd like all three to share the same 16bit space as follows:

- The 16bit variable should reside at memory location X.
- The first 8bit variable should reside at memory location X.
- The second 8bit variable should reside at memory location X+1.

Therefore:

- Changing the value of the 16bit variable will change the value of both 8bit variables
- Changing the value of the first 8bit variable will change the value of the low byte of the 16bit variable
- Changing the value of the second 8bit variable will change the value of the high byte of the 16bit variable

I know this is possible in some way or another using unions, but I'm after a way of doing things where I don't have to place the variables within a structure.

Any ideas appreciated, however wacky!

Cheers

Jon
Pejo Software - Per · 26 Aug 2003, 18:15 UTC
You could use pointers to accomplish that. But that was probably not what you wanted.
You can use bit-shifting to do this. This is how three color values, Red, Green, Blue, are stored in 16bits.

Say you have two 8-bit values, a and b and you want to store them in the a 16-bit variable c:

c = (a << 8) & b;

In PocketFrog, the RGB value of a 'Pixel' can be set using this function, found in defs.h:

Code:

typedef uint16_t Pixel;

const Pixel RED_MASK = 0x1F << 11;
const Pixel GREEN_MASK = 0x3F << 5;
const Pixel BLUE_MASK = 0x1F;

// Return a "Pixel" from the specified color components.
// Each component value must be between 0 and 255
inline Pixel Color( int red, int green, int blue )
{
return ((red << 8) & RED_MASK) | ((green << 3) & GREEN_MASK) | (blue >> 3);
}


Pam
Jont,

ignoring the "why?!", you could use a combination of an anonymous union and struct - that way you don't need the union/struct prefix, which is the reason (I'm assuming) that you didn't want the union/struct:

Code:
class botch{
public:
union {
unsigned short ushort; // accessed as object.ushort
struct {
char a; // accessed as object.a
char b; // accessed as object.b
};
};
};

int _tmain(int argc, _TCHAR* argv[])
{
botch temp;
temp.ushort=0x1234;

printf("ushort=%Xh\na=%Xh\nb=%Xh\n",temp.ushort, temp.a, temp.b);

return 0;
}


Spits out:
ushort=1234h
a=34h
b=12h

(I've not tested it in EVC++, but it works fine in VC++). YMMV.

Addendum:
http://msdn.microsoft.com/library/defau ... sTypes.asp

Cheers,

Ref.
Thanks for the replies folks.

I'm already aware of the bit shifting and pointer solutions - I'm currently using the pointer solution but I'm concerned about the overhead of fetching the pointer variable every time I want to retrieve the value.

Ref: I'm interested to see what sort of assembly would be generated by the anon class types solution. Like the pointer solution, I'm concerned that the compiler may produce additional code to what it needs to generate.
The union/struct solution should be the best solution in terms of performance. I see no reason for even an incredibly crappy compiler to do something else than simply access the data. The structs/unions do not cause any runtime computations. The compiler will see all variables a,b and ushort as having constant offsets just as they would if they were declared directly in the class itself. The difference is simply that with the use of union you can make the offsets overlap.

But, to really be able to tell what is best for performance it is necessary to consider how the variables are used. Frequently reading/writing a or b can be slower than reading ushort modifying it and writing it back. This is an optimisation that not all compilers will do automatically.
Just to say that my assessment is identical to Orpe's - i.e. there should be little or no overhead accessing the anonymous class/struct members - they're essentially "normal" variables (unless the compiler really is brain-dead about structs).

I can't check out the assembler 'cos I don't currently have an EVC++ installation due to my IBM DeskStar going DeathStar on me...

However, if you want to knock up a test function and dump the assembler output we can tell you how well the compiler is dealing with it.

Cheers,

Ref.