5 ms·
Why does it use std::ptrdiff_t for counting instead of int?
by threatripper 5y ago
Why does it use std::ptrdiff_t for counting instead of int?
- Google234 5y agoInt is not large enough for 64 bit system. ptrdiff Is always the correct size and is signed unlike size_t
- threatripper 5y agoAs far as I can see it's just used as a counter and doesn't really relate to pointers in any way. Why is int not large enough for this use case? EDIT: Answer from the developers here https://stackoverflow.com/a/45873222 https://stackoverflow.com/a/45873222 it's about compatibility with other types used for related purposes.
- amelius 5y agoWhat if you were counting bits instead of bytes?
- threatripper 5y agoBits don't appear in array sizes as we use bytes as smallest addressable unit on all relevant platforms.
- amelius 5y agoBut you could imagine a vector of booleans which you can subscript per boolean.
- threatripper 5y agoImaginable: Yes, Relevant: No
- hedora 5y agoCurrent intel 64 bit machines only have 57 bits of address space. The top bits of the pointers are wasted.
- smithza 5y agoI have made a point of using uint16_t, uint32_t, int16_t, int32_t, etc. to be explicit. Some don't like the look, but it is explicit and helpful for me.
- dgrunwald 5y agoThere are 64 bit systems where "long int" still is only 32 bits. (most importantly: Windows x64) In general, the C integer types cannot be relied upon; typedefs like uint32_t, size_t, ptrdiff_t should always be preferred.
- threatripper 5y agoYes, i see this as a shortcoming of C. Too many assumptions were crammed into the "int" size.
- mpfundstein 5y agoso why not use uint64_t ? makes semantically more sense, doesn't it?
- ncmncm 5y agostd::uint64_t is optionally supported. Strange but true. All actual machines do support it, but the Standard can't assume it's there.
- hibbelig 5y agoI'm surprised that negative numbers are needed here. But I don't know C++ and my knowledge about latches in Java is spotty at best. Don't know anything about barriers.
- TwelveNights 5y agoThe ptrdiff doesn't have to be signed. I'm pretty sure negative numbers aren't used in this context.
- RJIb8RBYxzAMX9u 5y agoProbably because ssize_t is not part of the C++ standard.
- codewiz 5y agoSome rationale against ssize_t is given here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1227r0.html#ssize_t http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p122...
- Glavnokoman 5y agowhy would it? More so I do not see why ptrdiff_t instead of size_t.
- bonzini 5y agoProbably so that implementations can use the spare bit for a flag, such as "is anyone waiting on this".
- gjulianm 5y agoptrdiff_t is signed, while size_t is unsigned. A size is only positive, a difference might be positive or negative, so you avoid overflow problems.
- Glavnokoman 5y agowhich difference?
- nickysielicki 5y agohttps://stackoverflow.com/a/45873222 https://stackoverflow.com/a/45873222 Answered by someone on the WG here.
- threatripper 5y agoThe argument is to use the same type as vector<thread>::difference_type to avoid surprises.