Background:I encountered this in GCC c++ standard library extension pool_allocator.h, which contains an allocator type that utilize memory pool to improve the efficiency of small chunk memory allocation. In the member function _M_refill()
of __pool_alloc_base
it need to deal with the type conversion from char*
(which is used for raw memory management) to _Obj*
(which is the list node struct used to manage the free chunk).
The situation is simpilified as following:I have a pointer p
to obj2 type, and I need to convert it to obj1 type, where obj1 and obj2 have totally no inheritance relationship. There are two ways to achieve this:
(obj1*)p
(obj1*)(void*)p
My question is:What is the differences between these two? Why would GCC choose the second way?
What I have tried:I have asked DeepSeekR1 about this, and he thinks there is no difference(all same as reinterpret_cast<obj1*>(p)
).I guess this might be related to some platform/compiler-specific problems and is out of safety concerns.