My understanding is that malloc()
should not fail unless it cannot allocate the amount of memory it was asked for. However, when you use it, you are required to release that memory when you don't need it anymore or else your program will request more and more memory and eventually the request will fail because you haven't released anything.
Also, not all illegal memory operations cause an error, for example (code edited 2013-03-08 based on Kludas's observation that the previous version wouldn't compile):
#include <stdio.h>
#include <string.h>
int main(void)
{
char myString[4];
strcpy(myString, "abcdefghijklmnopqrstuvwxyz"); /* This overflows the buffer */
/* but MIGHT NOT cause an */
/* immediate crash! */
printf("myString = [%s]\n", myString);
return 0;
}
This may-or-may-not trigger an error, but it is certainly illegal because I am writing 26 characters into an array that should only hold 4 items (three characters plus a terminating NULL).