Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

c++ - What (actually) happens, when a function with the warning "control reaches end of non-void function" is called?

I know what this message means, I just wondered why it is not an error message, but just a warning?

What happens in this case? For example, suppose I have a function

int f()
{
}

and what happens when I call it?
Does the compiler adds returning of "non-initialized" int in this case?
Or the missing return could cause stack corruption?
Or it's (absolutely) undefined behavior ?

Tested with gcc 4.1.2 and 4.4.3


EDIT: Reading the answers I understand one thing, reading the comments - another..

OK, let's summarize: it's undefined behavior. Then, this means, that it is possible to result in stack corruption, right? (it even means, that my computer may start throwing rotten tomatoes over me through the mic jack, screaming - "what have you done???").

But if so, then why the top answer here says, that stack corruption can't happen and, in the same time, that the behaviour is undefined?

And undefined in respect to? The caller, that tries to use the "not returned value", or just the end of the function is undefined, if it must return value, but it doesn't?

Or it's not undefined behavior, and just the user, who tries to use the value (that is not returned, d'oh!) will "receive" undefined value? In other words - just some garbage value and nothing more can happen?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A: No, the missing return would not cause stack corruption

A: Yes, the behavior would be "undefined" if the caller tried to read and/or use the (undefined!) return value.

PS:

Here's a citation for C++:

C++03 §6.6.3/2:

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...