OutpostUniverse / op2ext

Outpost 2 extension module loader
1 stars 0 forks source link

Change pointer integer types to `uintptr_t` #268

Closed DanRStevens closed 4 years ago

DanRStevens commented 4 years ago

I recently came across a few sources that point to the subtle distinction between std::size_t and uintptr_t. It seems some of our uses of std::size_t are perhaps more accurately handled by the uintptr_t type.

Technically pointers to objects and maximum size of objects can be two different types with two different sizes. It just so happens that in today's typical flat memory models, the two types coincide. Previously, on segmented memory models, this was not the case. For 16-bit x86 real mode programming, object size might be restricted to a 16-bit segment, while an address might include both a segment and an offset, resulting in a 32-bit pointer. Future architectures are similarly not restricted to have matching types/sizes.

Reference: Fixed width integer types (C99, included by C++11) What is uintptr_t data type

There are likely other instances throughout the project that could be updated.

DanRStevens commented 4 years ago

I expanded a bit on the use of uintptr_t. I also did a bit of reading where I found many examples of std::uintptr_t. That does indeed seem like the correct C++ way of doing things.

DanRStevens commented 4 years ago

Nah, you're right. I think the std:: prefix is right for C++. I just happened to only come across C examples when I first found the type.

I suppose it would have been nice if those primitive types could have been added to the language at the same time as int, short, and char. (They maybe they wouldn't need the _t suffix either). Oh well, what can you do. There's a good potential it could have broken existing code if they have added new names into the global scope. Preserving the ability to compile old code seems to have been a priority for the C++ extensions.