views:

51

answers:

3

The VS documentation states

Half the size of a pointer. Use within a structure that contains a pointer and two small fields.

What, exactly, is this type and how is it used, if ever?

A: 

I found this article on Intel's site, and it they suggest using it in a context where you have a class with many pointer members, along with a 32 bit offset to get the actual address, to cut down on data bloat of a class. The article specifically talks about the Itanium platform because it uses 64 bit pointers instead of 32 bit, but I assume the problem/solution to the problem would be the same on any system using 64 bit pointers.

So in short, it seems to suggest that it can be used if you, for example, wish to reduce the memory footprint of a class?

Jacob
+1  A: 

Note: Anonymous structs are not standard, but MSVC takes them:

union
{
    int * aPointer
    struct
    {
        HALF_PTR lowerBits;
        HALF_PTR upperBits;
    };
} myvar; //You can be assured this union is sizeof(int *)

If you're thinking they're not too terribly useful, you would be right.

Billy ONeal
+2  A: 

Use within a structure that contains a pointer and two small fields.

This means that in the following structure, no padding is required:

struct Example {
    void* pointer;
    HALF_PTR one;
    HALF_PTR two;
};

Of course, this is only relevant if the size of HALF_PTR (32 bits on a 64-bit system, 16 bits on a 32-bit system) is sufficient to hold the intended values.

Philipp
Forgot about the padding bit. +1.
Billy ONeal
This needs a union.
Hans Passant
@Hans Passant: Why does it need a union? You can be sure here that `sizeof(struct Example)` is `2*sizeof(void*)`. No need for a union here.
Billy ONeal
@Billy: well, you'd use HALF_PTR to access the parts of the pointer. Like you did in your answer.
Hans Passant
@Hans Passant: The documentation doesn't indicate that one should use `HALF_PTR` to access the parts of a pointer—after all, what would you gain from splitting up a pointer in two parts? I think the quotation indicates a usage like the one in my example.
Philipp
How do you propose to initialize them?
Hans Passant