Difference between LPVOID and void*

前端 未结 4 1511
一整个雨季
一整个雨季 2020-12-23 19:26

Can I use void* instead of LPVOID in C?

Or LPVOID perform some special functionality than void*.

相关标签:
4条回答
  • 2020-12-23 20:17

    LPVOID is simply a Windows API typedef for void*.

    0 讨论(0)
  • 2020-12-23 20:19

    LPVOID is

    typedef void* LPVOID
    

    defined in Windef.h where every windows datatypes were defined.

    We can use void* to point any type.

    0 讨论(0)
  • 2020-12-23 20:28

    LPVOID is a pointer to any type. This type is declared in WinDef.h as follows: typedef void *LPVOID;

    0 讨论(0)
  • 2020-12-23 20:31

    There is no LPVOID type in C, it's a Windows thing.

    And the reason those sort of things exists is so that the underlying types can change from release to release without affecting your source code.

    For example, let's say early versions of Microsoft's C compiler had a 16-bit int and a 32-bit long. They could simply use:

    typedef long INT32
    

    and, voila, you have your 32-bit integer type.

    Now let's go forward a few years to a time where Microsoft C uses a 32-bit int and a 64-bit long. In order to still have your source code function correctly, they simply change the typedef line to read:

    typedef int INT32
    

    This is in contrast to what you'd have to do if you were using long for your 32-bit integer types. You'd have to go through all your source code and ensure that you changed your own definitions.

    It's much cleaner from a compatibility viewpoint (compatibility between different versions of Windows) to use Microsoft's data types.

    In answer to your specific question, it's probably okay to use void* instead of LPVOID provided the definition of LPVOID is not expected to change.

    But I wouldn't, just in case. You never know if Microsoft may introduce some different way of handling generic pointers in future that would change the definition of LPVOID. You don't really lose anything by using Microsoft's type but you could be required to do some work in future if they change the definition and you've decided to use the underlying type.

    You may not think pointers would be immune to this sort of change but, in the original 8088 days when Windows was created, there were all sorts of weirdness with pointers and memory models (tiny, small, large, huge et al) which allowed pointers to be of varying sizes even within the same environment.

    0 讨论(0)
提交回复
热议问题