0

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)

6
  • 4
    references dont exist in C. Also all pointers are retrned by value, but NULL will not create temporary object. It basically means "nothing in here, pass along"-kinda thing Commented Oct 28, 2014 at 9:43
  • 1
    Can wrote string, did you mean std::string from C++/STL. If you are using C++ (and maybe C++11) please use nullptr Commented Oct 28, 2014 at 9:45
  • This looks just like your previous question Commented Oct 28, 2014 at 9:47
  • @DavidHeffernan look on answers. They are completely different. Somebody tells U cant return NULL, its temporary object, and somebody this is absolutly ok. Commented Oct 28, 2014 at 9:53
  • @Dima Read the final sentence here. Where you ask about references. Which is what you asked last time. My guess is that you actually are interested in references. Look at the code in this question. Who owns the object whose address is stored in ptr? Who is going to delete it? Commented Oct 28, 2014 at 9:59

4 Answers 4

13

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.

Sign up to request clarification or add additional context in comments.

5 Comments

so, as I understood, I can use it this way (to tell some error accured). Thanks a lot!
"Because NULL is a literal, no temporary object is created." Huh? You are assuming a lot about optimization and calling convention. More likely, the value NULL is pushed onto the function call stack.
@Lundin Thanks for your comment. I would try to rephrase my conclusion.
Actually std::string does have a constructor that initializes with a null pointer and, in GCC, it causes an instant crash (tries to read it in as a c-string).
@Galik What I mean is, no usable constructor. A compliant implementation is allowed to crash if you initialize the std::string with NULL
3

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 *

2 Comments

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 * so does it mean that ptr will ALWAYS be 0 if I return NULL in C.
@Dima It is will be null pointer value that can be implicitly converted to false in conditions: "A zero value, null pointer value, or null member pointer value is converted to false"
2

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.