I'm quite new at working with C++ and haven't grasped all the intricacies and subtleties of the language.
What is the most portable, correct and safe way to add an arbitrary byte offset to a pointer of any type in C++11?
SomeType* ptr; int offset = 12345 /* bytes */; ptr = ptr + offset; // <-- I found many answers on Stack Overflow and Google, but they all propose different things. Some variants I have encountered:
-
ptr = (SomeType*)(((char*)ptr) + offset); Cast to
unsigned int:ptr = (SomeType*)((unsigned int)ptr) + offset);-
ptr = (SomeType*)((size_t)ptr) + offset); "The size of
size_tandptrdiff_talways coincide with the pointer's size. Because of this, it is these types that should be used as indexes for large arrays, for storage of pointers and pointer arithmetic." - About size_t and ptrdiff_t on CodeProjectptr = (SomeType*)((size_t)ptr + (ptrdiff_t)offset);Or like the previous, but with
intptr_tinstead ofsize_t, which is signed instead of unsigned:ptr = (SomeType*)((intptr_t)ptr + (ptrdiff_t)offset);Only cast to
intptr_t, sinceoffsetis already a signed integer andintptr_tis notsize_t:ptr = (SomeType*)((intptr_t)ptr) + offset);
And in all these cases, is it safe to use old C-style casts, or is it safer or more portable to use static_cast or reinterpret_cast for this?
Should I assume the pointer value itself is unsigned or signed?
int a[5]; a + 5;is good,int a[5]; a + 6is bad). Edit: found a source: stackoverflow.com/a/988220/395760