In C++, is “return;” the same thing as “return NUL

2019-02-17 05:17发布

my question is return; the same as return NULL; in C++?

I understand that in C++, return NULL; is the same as return 0; in the context of pointers. Obviously for integers, this is not the case as NULL cannot be added, subtracted, etc. And that it is encouraged by some to use 0 instead of NULL for pointers because it is more convenient for portability. I'm curious if this is another instance where an equivalence occurs.

I suspect that they are equivalent because return; is saying return 'nothing' and NULL is 'nothing.' However, if someone can either confirm or deny this (with explanation, of course), I would be very grateful!

标签: c++ null return
5条回答
Viruses.
2楼-- · 2019-02-17 05:40

It works like a void function, where the return will simply exit the function and return nothing at all. This has been asked before.

查看更多
爷、活的狠高调
3楼-- · 2019-02-17 05:51

There is a difference in the resulting machine code:

return will just put the instruction pointer back to where the caller came from.

return 0 or return NULL will put 0 on the stack, and then put the instruction pointer back to where the caller came from. (In C, NULL is usually mapped to 0, although that can (may?) differ in different implementations. In C++, NULL is not supposed to be used).

查看更多
老娘就宠你
4楼-- · 2019-02-17 05:54

return with no expression works only if your function is declared void, in a constructor, or in a destructor. If you try to return nothing from a function that returns an int, a double, etc., your program will not compile:

error: return-statement with no value, in function returning ‘int’

According to §6.6.3/2 of C++11:

A return statement with neither an expression nor a braced-init-list can be used only in functions that do not return a value, that is, a function with the return type void, a constructor (12.1), or a destructor (12.4).

(thanks sftrabbit for the excellent comment).

查看更多
Bombasti
5楼-- · 2019-02-17 06:06

is return; the same as return NULL; in C++?

No.

return is used to "break" out from a function that has no return value, i.e. a return type of void.

return NULL returns the value NULL, and the return type of the function it's found in must be compatible with NULL.


I understand that in C++, return NULL; is the same as return 0; in the context of pointers.

Sort of. NULL may not be equivalent to 0, but it will at least convert to something that is.


Obviously for integers, this is not the case as NULL cannot be added, subtracted, etc.

You can perform addition and subtraction to pointers just fine. However, NULL must have integral type (4.10/1 and 18.1/4 in C++03) anyway so it's moot. NULL may very well be a macro that expands to 0 or 0UL.

Some modern compilers will at least warn you if it was actually NULL you wrote, though.


And that it is encouraged by some to use 0 instead of NULL for pointers because it is more convenient for portability. I'm curious if this is another instance where an equivalence occurs.

No. And I disagree with this advice. Though I can see where it's coming from, since NULL's exact definition varies across implementations, using NULL will make it much easier to replace with nullptr when you switch to C++11, and if nothing else is self-documenting.

查看更多
We Are One
6楼-- · 2019-02-17 06:06

return, return 0 and return NULL are not the same.

return is only valid with functions returning void, e.g.:

void func(...);

return 0 is for functions returning some int type, e.g.:

unsigned long func(...);

although it works with floating point types as well:

double func(...) { ... return 0; }

return NULL is for functions returning some pointer type, e.g.:

Something *func(...);
查看更多
登录 后发表回答