How to check if a void* pointer can be safely cast to something else?

前端 未结 6 2214
南旧
南旧 2020-12-19 08:32

Let\'s say I have this function, which is part of some gui toolkit:

typedef struct _My_Struct My_Struct;
/* struct ... */

void paint_handler( void* data )
{         


        
6条回答
  •  囚心锁ツ
    2020-12-19 08:42

    The best you could do would be to look at what data points to to see if it has telltale signs of being what you want, although a) it wouldn't be anywhere close to a guarantee and b) might be dangerous, as you don't know how big the thing data actually points to is. I suppose it isn't any more dangerous than just casting it and using it, but (as has been suggested) a redesign would be better.

提交回复
热议问题