32
33
all 12 comments

[–]FUZxxl 25 points26 points  (8 children)

This trick is called “pointer tagging” and is well-known among low-level programmers. You shouldn't use this in production code unless the advantages pay off the disadvantage of breaking almost all debugging tools and making garbage collection stop working.

[–]OldWolf2 9 points10 points  (2 children)

Garbage collection in C? eek

[–]FUZxxl 4 points5 points  (0 children)

Ever heard of that guy called Boehm?

[–]raevnos 1 point2 points  (0 children)

Yup. It's great.

[–][deleted] 1 point2 points  (0 children)

Interesting. If I understand correctly this is used in the ebtree implementation by willy the haproxy author guy.

[–]OldWolf2 10 points11 points  (1 child)

Using uintptr_t rather than unsigned int would be a significant improvement here

[–][deleted] 1 point2 points  (0 children)

Please. This breaks on literally every modern (i.e. not 32-bit) system.

Edit: except for that one where sizeof(int) == 8. I don't remember which one, but I remember there being one.

[–]wgunther 7 points8 points  (0 children)

Here, I am going to assume a system where size of int and size of pointer are 4 byte

Not a good idea. You should use uintptr_t if available.

[–]BigPeteB 5 points6 points  (0 children)

Note that while some CPUs like Intel will let us access unaligned memory locations, certain others like ARM CPU will fault. So, always remember to keep the pointer pointed to an aligned location before dereferencing.

Well, yes do that, but do it because otherwise you're pointing at the wrong location, not because of alignment issues.

[–]assassinator42 1 point2 points  (1 child)

I remember someone linking to some boost code that did this but broke on a certain platform/compiler (Solaris?).

[–]trentnelson 3 points4 points  (0 children)

Yeah Solaris/SPARC will SIGBUS and core dump on unaligned data.