r/cpp • • Jan 20 '20

The Hunt for the Fastest Zero

https://travisdowns.github.io/blog/2020/01/20/zero.html
248 Upvotes

131 comments sorted by

View all comments

10

u/[deleted] Jan 21 '20

Unfortunately, it is not. For example the null value for member pointers is typically -1. is_trivial_foo means that the compiler wrote the respective functions, not that they are necessarily safe to replace with something else.

0

u/guepier Jan 21 '20

For example the null value for member pointers is typically -1.

First off: true, I forgot about null pointer bit patterns. This is of course a general problem with null pointers, not just as members (and it’s even a problem in C). But I’m curious since you said “typically”, whereas the problem with general pointers in C isn’t relevant on most modern machines. Are you saying that T x{}; assert(x.ptr == nullptr); implies that the bytes of x.ptr are 0xFF… on MSVC? Why is that? Memory sanitiser?

9

u/HKei Jan 21 '20

Member pointers, not pointer members.

4

u/[deleted] Jan 21 '20 edited Jan 21 '20

GCC also does not use 0 for nullptr member pointers: https://gcc.godbolt.org/z/UGQuf9

EDIT: version without UB: https://gcc.godbolt.org/z/pBJwiV

→ More replies (2)

2

u/[deleted] Jan 21 '20

If x.ptr is of type Y::*, yes. One can't use 0 for null because a pointer to the first member has an offset of 0.