ShPavel
ShPavel

Reputation: 973

What is a proper way to return NOT NULL pointer

I use an external C library in my C++ program and the library uses a callback function, that must return void*. The library checks if return value is not NULL, which means success.

So what is the best way to tell it that all is fine?

I use:

return reinterpret_cast<void*>(1);

but it looks ugly...

Edit: thanks for reply, i will stay with this:

static int success;
return &success;

Upvotes: 8

Views: 3940

Answers (5)

Steve Jessop
Steve Jessop

Reputation: 279225

I'd probably write something like:

return const_cast<void*>(""); // non-null pointer, referand doesn't matter

Perhaps the callback function has a pointer input guaranteed not to be null. If so then you could return that.

reinterpret_cast<void*>(1) is supposed to look ugly, because it's not guaranteed to work. Most architectures don't mind you doing that, but

  • the standard doesn't actually guarantee that you can use a pointer value that isn't the address of any object (or a null pointer, or one-off-the-end of an array).
  • the standard doesn't guarantee that reinterpret_cast<void*>(1) is not a null pointer, that's implementation-defined.

I doubt there really is an implementation in which your function returns null by accident, but there's allowed to be. Imagine a hypothetical implementation in which the CPU's own addressing is 4-bit rather than 8-bit. Of course the implementer would still choose CHAR_BIT == 8, and all pointers in C++ would be 8-bit aligned. Then the implementation could legally and fairly reasonably map from pointers to integers by leaving the bit pattern unchanged, and from integers to pointers by zeroing the last bit. Alternatively it could right-shift and left-shift by 1 bit. The standard requires that pointer -> integer -> pointer restores the original value, but doesn't require that integer -> pointer -> integer does.

Upvotes: 3

Moha Dehghan
Moha Dehghan

Reputation: 18443

If it were me, I would use:

return (void*)1;

Simple and beautiful.

I prefer value 1 (as you used in question) over any other value, because other values can be interpreted in some way (which is not good). If all you want is to return a non-zero value (non-null pointer) it is better that you use a value that nothing special can be extracted from it.

Upvotes: 1

Marcelo Cantos
Marcelo Cantos

Reputation: 185842

static int dummy;
return &dummy;

Strictly speaking, static is probably not required, but it feels a bit gauche to return a pointer to a local that's going out of scope.

EDIT: Note @sharptooth's comment. Strictly speaking, static is required.

Upvotes: 12

simonc
simonc

Reputation: 42165

How about

return this;

as an easy way to return a valid pointer that is guaranteed to be non-NULL?

Upvotes: 2

sharptooth
sharptooth

Reputation: 170489

Using reinterpret_cast is as clean as you could get to. The problem is crafting such pointers from int literals yields undefined behavior - the Standard forbids use of pointers that are not null and are not pointing to properly allocated objects. The Standard-compliant way would be to have some variable (perhaps a file scope variable would be the most convenient) and return its address.

Upvotes: 4

Related Questions