What is uintptr_t data type

后端 未结 5 718
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 04:15

What is uintptr_t and what can it be used for?

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 04:59

    There are already many good answers to the part "what is uintptr_t data type". I will try to address the "what it can be used for?" part in this post.

    Primarily for bitwise operations on pointers. Remember that in C++ one cannot perform bitwise operations on pointers. For reasons see Why can't you do bitwise operations on pointer in C, and is there a way around this?

    Thus in order to do bitwise operations on pointers one would need to cast pointers to type unitpr_t and then perform bitwise operations.

    Here is an example of a function that I just wrote to do bitwise exclusive or of 2 pointers to store in a XOR linked list so that we can traverse in both directions like a doubly linked list but without the penalty of storing 2 pointers in each node.

     template 
     T* xor_ptrs(T* t1, T* t2)
     {
         return reinterpret_cast(reinterpret_cast(t1)^reinterpret_cast(t2));
      }
    

提交回复
热议问题