Search code examples
c++heapalloc

"LPVOID" can not be assigned to an entity of the type X


I am dealing with a function that deals with USB devices.

But I am already having a problem at something simple:

I am getting the compiler error

A value of the type "LPVOID" can not be assigned to an entity of the type "PSP_DEVICE_INTERFACE_DETAIL_DATA" in the line 
"DevIntfDetailData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);"

Could anybody please tell me what I am doing wrong here? Thank you!

PSP_DEVICE_INTERFACE_DETAIL_DATA DevIntfDetailData;
DevIntfDetailData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);

Solution

  • That have to work:

    DevIntfDetailData = reinterpret_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize));
    

    HealAlloc (like all other alloc function) returns pointer to memory heap (void*), and C++ can't allow set value of type T* to value of type void* without manual casting.

    There exist dynamic_cast, static_cast, reinterpret_cast and const_cast. When you want to convert void* to any pointer, you have to use reinterpret_cast, because it converts types without any checking (converting void* to any T* will not pass any check).