When is an integer<->pointer cast actually correct?

后端 未结 15 2661
北荒
北荒 2020-12-13 07:48

The common folklore says that:

  • The type system exists for a reason. Integers and pointers are distinct types, casting between them is a malpractice in the m

15条回答
  •  清歌不尽
    2020-12-13 08:34

    Pointer values can also be a useful source of entropy for seeding a random number generator:

    int* p = new int();
    seed(intptr_t(p) ^ *p);
    delete p;
    

    The boost UUID library uses this trick, and some others.

提交回复
热议问题