|
From: Adam J. <aj...@i-...> - 2007-07-17 16:11:20
|
>>I really don't understand your talk about malloc() and checking for >>sucessful allocation here. >>If doing >> char str = (char*)malloc(10); >>then the allocation may fail, the the application should test: >> if (str == NULL) fail; >>or >> if (!str) fail; >>However, when declaring: >> char str[10]; >>then the only way the application can fail is if the operating system >>can't supply enough stack space. There are no pointer to test. If the >>allocation fails (the stack can't fit the variable) then the >>application will die. The whole point is to be able to exit the program gracefully and not to hard crash your program. As I stated in my post. Here is what I wrote in the post again: >>You also assume which is > >not very good that the system will allocate a char str[10] without > >checking to see if it can do it with a malloc first. On memory > >allocations you need to check first, and if it fails exit your program gracefully. You assume that letting the system crash is okay. That may be true in the desktop world were you have lots of memory and the person can just reboot their system or kill the program. This is not so true in a mission critical environment were a crash is fatal to a multimillion dollar satellite. In those situations when the malloc fails, You can signal a second system to take over, while controllers on the ground figure out what went wrong with the first system. You just can't let the system crash.... You have to handle it. I admit his example is trivial, but it is a good habit to get into and that was my point..... |