What's the difference between a null pointer and a void pointer?

前端 未结 11 1605
甜味超标
甜味超标 2020-12-02 05:13

Whats the difference between a Null pointer & a Void pointer?

11条回答
  •  既然无缘
    2020-12-02 05:47

    Usually a null pointer (which can be of any type, including a void pointer !) points to:

    • the address 0, against which most CPU instructions sets can do a very fast compare-and-branch (to check for uninitialized or invalid pointers, for instance) with optimal code size/performance for the ISA.

    • an address that's illegal for user code to access (such as 0x00000000 in many cases), so that if a code actually tries to access data at or near this address, the OS or debugger can easily stop or trap a program with this bug.

    A void pointer is usually a method of cheating or turning-off compiler type checking, for instance if you want to return a pointer to one type, or an unknown type, to use as another type. For instance malloc() returns a void pointer to a type-less chunk of memory, the type of which you can cast to later use as a pointer to bytes, short ints, double floats, typePotato's, or whatever.

提交回复
热议问题