How to Print the Value of Nullptr on Screen

Unable to print the value of nullptr on screen

The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t.

Type nullptr_t should be convertible to T*, but compiler has no operator << for nullptr_t and don't know to which type you want to convert nullptr.

You can use this

cout << static_cast<void*>(nullptr) << endl;

Why can std::cout print the value of nullptr only if returned by a function?

Here is the meaning of nullptr from the CPP Standards [4.10]

  • A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to 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.

Something the CPP Standards specifies, is that it isn't a std::nullptr_t value, it evaluates to zero or a prvalue of type std::nullptr_t.

In your case, it's pretty much is an (TYPE) pointer to some extent, so when you try to paste it out, it pastes out it's type's value (Where it points to, but by itself it won't). Your function returns an int*, and you give it a nullptr. So what you are basically doing is giving an int* a value. nullptr by itself won't have a value but

int* abc = nullptr;

will.

printing nullptr with std::cout and printf

It's undefined behaviour

In both cases you are printing a C-style-string (a const char *). So the implementation needs to find the length of this string and searches the terminating null character in it. But the pointer is invalid, so you get undefined behaviour.

Technically both cases are UB, but the implementation of printf you are using seems to be recognizing the invalid pointer and prints (null) instead, whereas std::cout just crashes.

(Or maybe it also recognizes the invalid pointer and gracefully sets the fail bit, thus ignoring further operations. But ultimately it's UB, so anything can happen here.)

operator (stream output) for nullptr

This is LWG #2221, which proposes:

The obvious library solution is to add a nullptr_toverload, which would be defined something like

template<class C, class T>
basic_ostream<C, T>& operator<<(basic_ostream<C, T>& os, nullptr_t)
{
return os << (void*) nullptr;
}

We might also consider addressing this at a core level: add a special-case language rule that addresses all cases where you write f(nullptr) and f is overloaded on multiple pointer types. (Perhaps a tiebreaker saying that void* is preferred in such cases.)

It isn't in C++14, I don't know whether or not it will make it into C++17. It is a very easy problem to fix yourself, so it's not particularly high priority as far as standards changes go. As you said yourself in the question - it's just a 3 line function.

What is the behavior of printing NULL with printf's %s specifier?

First things first: printf is expecting a valid (i.e. non-NULL)
pointer for its %s argument so passing it a NULL is officially
undefined. It may print "(null)" or it may delete all files on your
hard drive--either is correct behavior as far as ANSI is concerned
(at least, that's what Harbison and Steele tells me.)

That being said, yeah, this is really wierd behavior. It turns out
that what's happening is that when you do a simple printf like this:

printf("%s\n", NULL);

gcc is (ahem) smart enough to deconstruct this into a call to
puts. The first printf, this:

printf("test %s\n", NULL);

is complicated enough that gcc will instead emit a call to real
printf.

(Notice that gcc emits warnings about your invalid printf argument
when you compile. That's because it long ago developed the ability to
parse *printf format strings.)

You can see this yourself by compiling with the -save-temps option
and then looking through the resulting .s file.

When I compiled the first example, I got:

movl    $.LC0, %eax
movl $0, %esi
movq %rax, %rdi
movl $0, %eax
call printf ; <-- Actually calls printf!

(Comments were added by me.)

But the second one produced this code:

movl    $0, %edi    ; Stores NULL in the puts argument list
call puts ; Calls puts

The wierd thing is that it doesn't print the following newline.
It's as though it's figured out that this is going to cause a segfault
so it doesn't bother. (Which it has--it warned me when I compiled
it.)

Returning NULL value from a function to a pointer in C

There are few bugs in your code. Corrections would be

for(i = 0; sentence[i] && wordCount < words; i++)

Another is

while (inputString[i] !=' ' && inputString[i]!='\0')

And the last

if (words > wordCount)

Explanation for first one would be - you wont check past the end of the string. Otherwise you had undefined behavior.

There maybe cases when you reach the end of the string but still won't get a space. To avoid that case you need to consider the \0 case too.

If the words > wordCount then only you should throw the error. If they are equal then it should print the value.

SimpleDateFormat returning null but can print date on screen

You are declaring a second variable named date in your try body. Remove the Date portion (which makes it local). Change

Date date = format.parse(sDate);

to

date = format.parse(sDate);


Related Topics



Leave a reply



Submit