C++ - Check if pointer is pointing to valid memory (Can't use NULL checks here)

后端 未结 6 1386
名媛妹妹
名媛妹妹 2020-12-21 18:01

I am creating scripting language. When I allocate thing ,it\'s allocate the thing and returns the address and then I do whatever with it and then delete it. I can\'t contro

6条回答
  •  天涯浪人
    2020-12-21 18:35

    The simplest solution is to use a map. The map should be indexed by a pointer to the object, likely as a void *. The contents of the map item should be the type of object created.

    Any time a script creates an object, add an entry to the map. Any time a script deletes an object, remove the map entry. When a script accesses an object, find the pointer in the map, thus confirming both that it exists and that the type is correct.

提交回复
热议问题