Void ** a generic pointer?

前端 未结 3 1571
北海茫月
北海茫月 2020-12-05 18:11

void * is a generic pointer, but what about void **? Is void ** also a generic pointer?

Can we typecast void ** t

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 19:14

    void** is a pointer to void*. On top of it being undefined behavior (which while scary, the behavior is often in effect defined by your compiler), it is also a huge problem if you do the reinterpret_cast:

    int x = 3;
    char c = 2;
    int* y = &x;
    void* c_v = &c; // perfectly safe and well defined
    void** z = reinterpret_cast(&y); // danger danger will robinson
    
    *z = c_v; // note, no cast on this line
    *y = 2; // undefined behavior, probably trashes the stack
    

    Pointers to pointers are very different beasts than pointers. Type changes that are relatively safe on pointers are not safe on pointers to pointers.

提交回复
热议问题