int fun()
{
printf("\ncrap");
}
void main()
{
printf("\n return value of fun %d", fun());
}
and please, can you explain on how the stack allocates the memory for return values and how the stack works here.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
funtriggers undefined behaviour.Please always compile with all compiler warnings enabled. That should give you a warning that you are making that very mistake.
Your
mainis also triggering undefined behaviour because the C++ standard demands that there be only one single function calledmainand it returnint. However, you are allowed, as a special case, to omit thereturnstatement in your (corrected)mainfunction.“The stack”, as you presume, is not part of the C++ language. But that’s irrelevant; the standard says that the returned object is constructed in the scope of the caller, which is all you need to know.
(Practically, an unreturned
intis probably going to end up like an uninitialized variable of typeint, but the standard says that the function call already triggers the undefined behaviour, not just read-access later on.)