To be more precise, the way that the underlying operating system lets a program know that it cannot allocated the requested memory is by returning NULL
from a call to malloc
. Robust programs should always check for that condition before using the returned value as a pointer or they will risk a crash by dereferencing a NULL pointer dereferencing a NULL pointer.
All versions of both standards since then (C99 and C++98) have maintained the same idea. We rely on automatically generated member functions in C++, and few people write explicit return;
statements at the end of a void
function. Reasons against omitting seem to boil down to "it looks weird" "it looks weird". If, like me, you're curious about the rationale for the change to the C standard read this question read this question. Also note that in the early 1990s this was considered "sloppy practice" because it was undefined behavior (although widely supported) at the time.
To be more precise, the way that the underlying operating system lets a program know that it cannot allocated the requested memory is by returning NULL
from a call to malloc
. Robust programs should always check for that condition before using the returned value as a pointer or they will risk a crash by dereferencing a NULL pointer.
All versions of both standards since then (C99 and C++98) have maintained the same idea. We rely on automatically generated member functions in C++, and few people write explicit return;
statements at the end of a void
function. Reasons against omitting seem to boil down to "it looks weird". If, like me, you're curious about the rationale for the change to the C standard read this question. Also note that in the early 1990s this was considered "sloppy practice" because it was undefined behavior (although widely supported) at the time.
To be more precise, the way that the underlying operating system lets a program know that it cannot allocated the requested memory is by returning NULL
from a call to malloc
. Robust programs should always check for that condition before using the returned value as a pointer or they will risk a crash by dereferencing a NULL pointer.
All versions of both standards since then (C99 and C++98) have maintained the same idea. We rely on automatically generated member functions in C++, and few people write explicit return;
statements at the end of a void
function. Reasons against omitting seem to boil down to "it looks weird". If, like me, you're curious about the rationale for the change to the C standard read this question. Also note that in the early 1990s this was considered "sloppy practice" because it was undefined behavior (although widely supported) at the time.
To be more precise, the way that the underlying operating system lets a program know that it cannot allocated the requested memory is by returning NULL
from a call to malloc
. Robust programs should always check for that condition before using the returned value as a pointer or they will risk a crash by dereferencing a NULL pointer .
Similarly, scanf
returns the number of arguments successfully scanned. In your case it should be 1
because each call is scanning one integer value. If it's anything other than 1
, it's an indication of a problem that should probably be handled gracefully by the program, perhaps by emitting a message to the user that only integer values are allowed and asking again.
To be more precise, the way that the underlying operating system lets a program know that it cannot allocated the requested memory is by returning NULL
from a call to malloc
. Robust programs should always check for that condition before using the returned value as a pointer or they will risk a crash by dereferencing a NULL pointer .
Similarly, scanf
returns the number of arguments successfully scanned. In your case it should be 1
because each call is scanning one integer value. If it's anything other than 1
, it's an indication of a problem that should probably be handled gracefully by the program, perhaps by emitting a message to the user that only integer values are allowed and asking again.
So for example, when I feed the program this input:
10 5 33 2 18 17 15 20 22 1 8
The program then constructs this tree:
And an in-order traversal produces this output:
1 2 5 8 10 15 17 18 20 22 33
So for example, when I feed the program this input:
10 5 33 2 18 17 15 20 22 1 8
The program then constructs this tree:
And an in-order traversal produces this output:
1 2 5 8 10 15 17 18 20 22 33