Should I cast size_t to ptrdiff_t?

1.6k Views Asked by At

I have a malloc'ed array of pointers that forms a hash table. To step through the hash table I'd use pointer arithmetic, eg:

node_t ** tc = table;
size_t tcs = sizeof(node_t *);
for(long i = 0; i < tableSize; tc+=tcs, ++i) { 
    // Do some stuff with *tcs location in the table.
}

Question is should I cast the size_t returned by sizeof() to ptrdiff_t for correct addition in the increment part of the for condition? or does it even matter for addition?

3

There are 3 best solutions below

0
On BEST ANSWER

No need for ptrdiff_t here as there is no pointer difference around.

What you probably want is:

for (node_t ** tc = table; tc < (table + tableSize); ++tc) {
  ...
}
0
On

No. You don't need to.

Pointer arithmetic is performed based on the type of the pointer T *. Adding a size_t will not affect the pointer arithmetic as the increment is done using sizeof(T).

To quote the standard (C11 draft):

6.5.6 Additive operators

When an expression that has integer 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 integer 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 the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

On the other hand, casting size_t to ptrdiff_t could lead incorrect code as ptrdiff_t is a signed while size_t is an unsigned type. So if the resulting value is larger than what ptrdiff_t can hold then there's a problem. In short, pointer arithmetic is well-defined when adding any integral type to a pointer type and you don't need such a cast at all.

0
On

You need ptrdiff_t for handling negative values. size_t handles positive values, such as the result of sizeof in your example, so you do not need a cast.

With this said, the code looks suspicious: C compiler factors the size of the struct into your pointer arithmetics, so the fact that you have to add sizeof(node_t*) to a double-pointer to node_t is probably a mistake. If you want to advance to the next pointer in an array of pointers, add 1 to the current value of that pointer. The compiler is smart enough to multiply that 1 by sizeof(*ptr) for you, based on the type of the pointer.