Is there an integral type that has the same size and alignment as a pointer?
Asked Answered
L

1

8

Is there an integral type that has the same size and alignment as a pointer?

There are std::intptr_t and std::uintptr_t, but I believe they don't have the same alignment as a pointer per se.

Lizalizabeth answered 27/11, 2015 at 18:28 Comment(4)
What difference does it make? Aliasing a pointer with an integral type is undefined behavior anyway.Acrylonitrile
@BenVoigt I'm not doing that. Rather, I'm computing data structures layouts at runtime, for a virtual machine implementation, and it is easier if integers have the same layout as pointers.Lizalizabeth
But if you are doing structure layout at runtime, the alignment should not be an issue: just add explicit padding fields everywhere the supossed alignment requirements matter and you should be fine.Barbate
You might want to use a union. While that's rarely useful, virtual machines are one of those cases in which they can make sense.Highlander
A
6

It's not required that all pointer types have the same size and alignment, which makes it impossible for any single integer type to have the same size and alignment as all pointer types.

For certain, object pointers, function pointers, and pointer-to-member have different requirements. But on some platforms, even char* and int* have different size.

Acrylonitrile answered 27/11, 2015 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.