What would be a proper invalid value for a pointer?

前端 未结 7 584
抹茶落季
抹茶落季 2021-01-25 03:16

Suppose I have this code. Your basic \"if the caller doesn\'t provide a value, calculate value\" scenario.

void fun(const char* ptr = NULL)
{
   if (ptr==NULL) {         


        
7条回答
  •  走了就别回头了
    2021-01-25 03:54

    Depending on your platform, a pointer is likely either a 32 or 64-bit value.

    In those cases, consider using:

    0xFFFFFFFF or  0xFFFFFFFFFFFFFFFF
    

    But I think the bigger question is, "How can NULL be passed as a valid parameter?"

    I'd recommend instead having another parameter:

    void fun(bool isValidPtr, const char* ptr = NULL)
    

    or maybe:

    void fun( /*enum*/ ptrState, const char* ptr = NULL)
    

提交回复
热议问题