You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While working on #85, we discussed the necessity of ptrdiff_t. In general, it's advised to use it instead of a pointer in pointer arithmetic if a value can ever become negative.
In uthash, ptrdiff_t type is used exclusively for calculating the distance between the beginning of the hashed structure and the location of the hash handle inside the structure.
There are 2 instances in the code where the pointers are subtracted. Any addition that is happening is between two positive values. In both instances where the subtraction is done, the subtraction is always between &PTR->HH and PTR, which guarantees that the result is not negative.
The question would be - what to replace it with, though. There is no universally guaranteed C type that is precisely the size of the pointer. long has been often used, but I bet there are targets where this is not the case (C standard does not require that). I would replace it with void*, but I'm not sure that all C compilers support pointer arithmetic between two void*.
The text was updated successfully, but these errors were encountered:
I'm afraid I'm not much help here- its been a long time since the ptrdiff type was added to uthash. I don't remember exactly why I adopted it. My intuition is that it's overkill. I have some code in my tpl library that uses uintptr_t. Would that be any better?
Hello.
While working on #85, we discussed the necessity of ptrdiff_t. In general, it's advised to use it instead of a pointer in pointer arithmetic if a value can ever become negative.
In uthash, ptrdiff_t type is used exclusively for calculating the distance between the beginning of the hashed structure and the location of the hash handle inside the structure.
There are 2 instances in the code where the pointers are subtracted. Any addition that is happening is between two positive values. In both instances where the subtraction is done, the subtraction is always between
&PTR->HH
andPTR
, which guarantees that the result is not negative.The question would be - what to replace it with, though. There is no universally guaranteed C type that is precisely the size of the pointer.
long
has been often used, but I bet there are targets where this is not the case (C standard does not require that). I would replace it withvoid*
, but I'm not sure that all C compilers support pointer arithmetic between twovoid*
.The text was updated successfully, but these errors were encountered: