If you have the following code where p is a pointer:
p = p + strlen(p) + size_t(1);
Since strlen()
and size_t
are both size_t
, should I cast the code to ptrdiff_t
?
p = p + (ptrdiff_t)(strlen(p) + size_t(1));
If so why?
Thanks, Greg
Use size_t for variables that model size or index in an array. size_t conveys semantics: you immediately know it represents a size in bytes or an index, rather than just another integer. Also, using size_t to represent a size in bytes helps making the code portable. Save this answer.
When writing C code you should always use size_t whenever dealing with memory ranges. The int type on the other hand is basically defined as the size of the (signed) integer value that the host machine can use to most efficiently perform integer arithmetic.
ptrdiff_t type is a base signed integer type of C and C++ language. The type's size is chosen so that it can store the maximum size of a theoretically possible array of any type. On a 32-bit system ptrdiff_t will take 32 bits, on a 64-bit one 64 bits.
On a typical 64-bit system, the size_t will be 64-bit, but unsigned int will be 32 bit. So we cannot use them interchangeably. One standard recommendation is that the size_t be at most as big as an unsigned long.
std::ptrdiff_t is signed. std::size_t is unsigned. Casting strlen(p)
to ptrdiff_t
would make sense if p
could have a negative length, which is not possible.
However, that cast could overflow the resulting signed value if p
is large enough (for instance, larger than 2,147,483,647 bytes on most 32-bit platforms). So it could introduce an error in your pointer arithmetic.
Best to stick with size_t
here.
There's no need to cast to ptrdiff_t
. Pointer arithmetic is well-defined for all integral types, including size_t
, and if size_t
wasn't big enough to hold the value, the cast to ptrdiff_t
comes too late anyway.
Here is the relevant language from the Standard (C++0x FCD, section [expr.add]
):
When an expression that has integral type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integral expression. In other words, if the expression P points to the i -th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n ) point to, respectively, the i + n -th and i − n -th elements of the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With