Attempting to dereference an invalid a NULL pointer results in undefined behavior, typically abnormal program termination. Given this, pointers should be checked to make sure they are valid before they are dereferenced.
...
In this example, input_str
is copied into dynamically allocated memory referenced by str
. If malloc()
fails, it returns an invalid (null) a NULL pointer that is assigned to str
. When str
is dereferenced in strcpy()
, the program behaves in an unpredictable manner.
...