|
|
|
|
|
by inkyoto
263 days ago
|
|
A null pointer is not a valid pointer in a predominant number of systems in existence. If malloc (3) has returned a NULL, *ptr will cause a SIGSEGV. Embedded systems are an exception, though. They may not have a MMU, and in such a case the operation will succeed. |
|
This code will NEVER deference a null pointer. Not under any compiler, not with any compiler options:
> A null pointer is not a valid pointer in a predominant number of systems in existence.No, that's not quite pedantically accurate. A null pointer is not a valid pointer in the C programming language. Address zero may or may not be, that's outside the scope of the C language. Which is why embedded and kernel work sometimes has to be very careful here.
> They may not have a MMU, and in such a case the operation will succeed.
Lack of MMU does not mean address zero is valid. It definitely* doesn't make a null pointer valid. In fact, a null pointer may not point to address zero.