PepeHands
PepeHands

Reputation: 1406

returning NULL pointer in C

I had some different answers on this question before, so decided to ask again here.

Suppose I have a function node* foo() and if some fail accured, I do return NULL. Does this code really return NULL pointer? Or is this NULL a local temporary object? I have no warnings while I compile it. Moreother I can write something like this:

node* ptr = foo();
if (ptr)
    printf("Not NULL");
else
    printf("NULL");

And it seems like works. (but if this function is, for example const string& foo() it doesn't)

Upvotes: 0

Views: 10946

Answers (4)

Gyapti Jain
Gyapti Jain

Reputation: 4106

NULL is a pointer literal which is defined to contain a special value.

One possible definition is:

#define NULL ((void *)0)

For more detail you can read this faq

About const string& foo(), I believe you mean C++'s std::string. std::string has no implicit constructor that initialize it with NULL pointer. So you should use some exception or empty string to indicate an error to the caller. (If you are not throwing, you must return an std::string. Even if the object returned is local, its life is prolonged when kept in a local constant reference. But returning some other type and expecting an implicit conversion is not a good idea.)

Answer to your question: Because NULL is a literal, no temporary object may be created most of the time and actual value can be directly returned to the caller.

Upvotes: 13

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

This function

const string& foo(); 

does not return a pointer. It returns a constant reference to an object of type std::string. So its return value may not be assigned to a pointer.

According to the C++ Standard

4.10 Pointer conversions [conv.ptr]

1 A null pointer constant is an integer literal (2.14.2) with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion. Two null pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion (4.4). A null pointer constant of integral type can be converted to a prvalue of type std::nullptr_t. [ Note: The resulting prvalue is not a null pointer value. —end note ]

So when you use null pointer constant defined with macro NULL it is assigned to the return pointer of the function that will contain null pointer value of type node *

Upvotes: 3

soerium
soerium

Reputation: 583

Generally NULL literal means that pointer is pointing to nothing - nothing is conventionally represented by value 0. Whatever it will be, C or C++.

Upvotes: 2

Lundin
Lundin

Reputation: 213862

When you return NULL, or anything else, the return value is likely saved on the stack (or in a CPU register). So you get a temporary "anonymous" variable there, which only exists on the line where you call the function.

This is done "between the lines" by the compiler, in some implementation-defined manner. Don't confuse this with the scope of local variables inside a function.

And of course, the compiler might decide to optimize away the whole parameter/return value shuffling, by doing some form of inline function optimization on your code.

As for returning a reference, it doesn't make any sense to set a reference to NULL. References should point at some allocated memory. If you have a function returning a reference, it most likely points at one of the passed parameters. Returning NULL is a simple C way of error handling. In C++ you have more options, like throwing an exception.

Upvotes: 1

Related Questions