1

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) {
      // calculate what ptr value should be
   }
   // now handle ptr normally
}

and call this with either

fun();          // don't know the value yet, let fun work it out

or

fun(something); // use this value

However, as it turns out, ptr can have all kinds of values, including NULL, so I can't use NULL as a signal that the caller doesn't provide ptr.

So I'm not sure what default value to give ptr now instead of NULL. What magic value can I use? Does anybody have ideas?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • 1
    A pointer should either point at a valid object or be null -- anything else is asking for trouble. Maybe you want [Boost.Optional](http://www.boost.org/libs/optional/)? – ildjarn May 07 '12 at 21:17
  • 1
    What exactly would be the difference between providing `NULL` and not providing ptr ? How does the function 'handle' NULL normally, and how would the handling differ when ptr is 'not provided' – Remus Rusanu May 07 '12 at 21:18
  • In my original concept, there wouldn't be a difference between `fun();` and `fun(NULL);`. Because I wasn't planning on using NULL. But now I do have to able to do `fun(NULL);` and have it processed normally. That's the problem. Edited my question. – Mr Lister May 07 '12 at 21:29

7 Answers7

4
void fun()
{
   // calculate what ptr value should be
   const char* ptr = /*...*/;

   // now handle ptr normally
   fun(ptr);
}
ronag
  • 49,529
  • 25
  • 126
  • 221
  • I think that's what I'm after, yes. You can name the other function the same though, as irobot pointed out later. – Mr Lister May 07 '12 at 21:52
2

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)
abelenky
  • 63,815
  • 23
  • 109
  • 159
2

I agree with all the other answers provided, but here's one more way of handling that, which to me personally looks more explicit, if more verbose:

void fun()
{
  // Handle no pointer passed
}

void fun(const char* ptr)
{
  // Handle non-nullptr and nullptr separately
}
irobot
  • 1,026
  • 1
  • 6
  • 13
  • 1
    I prefer this it separates the expected behaviour and checks from one function body to another and keeps the second definition cleaner and less error prone, with the empty arg version you just do the default thing or return – EdChum May 07 '12 at 21:37
1

You should use the nullptr for that. Its new in the C++11 standart. Have a look here for some explanation.

Community
  • 1
  • 1
Haatschii
  • 9,021
  • 10
  • 58
  • 95
1

Using overloaded versions of the same function for different input is best, but if you want to use a single function, you could make the parameter be a pointer-to-pointer instead:

void fun(const char** ptr = NULL) 
{ 
   if (ptr==NULL) { 
      // calculate what ptr value should be 
   } 
   // now handle ptr normally 
} 

Then you can call it like this:

fun();

.

char *ptr = ...; // can be NULL
fun(&ptr);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

If you want a special value that corresponds to no useful argument, make one.

header file:

extern const char special_value;

void fun(const char* ptr=&special_value);

implementation:

const char special_value;

void fun(const char* ptr)
{
    if (ptr == &special_value) ....
}
dave
  • 131
  • 2
0

1?

I can't imagine anyone allocating you memory with that address.

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
  • Hm, you know, I've actually been thinking about that, but no. My programmer's pride forbids it. It just doesn't feel right. – Mr Lister May 07 '12 at 21:33