Kristian D'Amato
Kristian D'Amato

Reputation: 4046

HALF_PTR Windows data type

The VS documentation states

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

Windows Data Types

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

Upvotes: 3

Views: 485

Answers (4)

Jacob
Jacob

Reputation: 3686

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?

Upvotes: 2

phuclv
phuclv

Reputation: 41932

I guess “Use within a structure that contains a pointer and two small fields” means a pointer constructed from two HALF_PTRs along with two other non-pointer small data fields

struct Packed {
    HALF_PTR low_ptr;
    HALF_PTR high_ptr;
    SMALL one;
    SMALL two;
};
struct Padded {
    void *ptr;
    SMALL one;
    SMALL two;
};
  • On 32-bit Windows:
    • When SMALL is char: sizeof(Packed) == 6 but sizeof(Padded) = 8
    • When SMALL is short: the size of both structs are 8, but the alignment requirement for the former is just 2 compared to 4
  • On 64-bit Windows:
    • When SMALL is char: sizeof(Packed) == 10 but sizeof(Padded) = 16
    • When SMALL is short: sizeof(Packed) == 12 but sizeof(Padded) = 16
    • When SMALL is int: same size, but reduced alignment requirement like above

This is unlike Philipp's answer where the size and alignment of the struct is exactly the same whether splitting into half or not

struct Example1 {
    void* pointer;
    HALF_PTR one;
    HALF_PTR two;
};
struct Example2 {
    void* pointer;
    void* one_two;
};

Both have alignment equal to the size of the pointer, and size of 2 pointers

Upvotes: 0

Philipp
Philipp

Reputation: 49842

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.

Upvotes: 1

Billy ONeal
Billy ONeal

Reputation: 106589

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.

Upvotes: 2

Related Questions