Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ptrdiff_t evaluation #86

Open
veselov opened this issue Jul 13, 2016 · 1 comment
Open

ptrdiff_t evaluation #86

veselov opened this issue Jul 13, 2016 · 1 comment

Comments

@veselov
Copy link
Contributor

veselov commented Jul 13, 2016

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 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*.

@troydhanson
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants