What is the difference (or causes) between a program that crashes and a program that hangs (becomes unresponsive) in C++?
For sure, accessing invalid memory causes a program to crash. Deadlock in threads may cause a program to hang. What are the other causes?
Does exhausting all memory causes a program to hang? or crash? I'm a bit confused between the differences and their causes.
Crashing is normally caused by an illegal instruction, e.g. accessing invalid memory, dividing by zero, etc. Usually this manifests itself as a well-known exception which is handled by the operating system.
Hanging can be broken up into 2 fairly high level categories:
Update based on question comment
A memory leak can cause a program to crash, but this depends on various factors:
Memory leaks may result in 2 bad things - a continual increase in memory usage by the process, and memory fragmentation. Both of these can result in failure to allocate memory down the line, if the OS cannot provide a contiguous block of memory.
In C++, if the
newoperator fails to allocate memory, a
std::bad_allocexception will be thrown. This will most likely be caught by the OS, resulting in a crash (unless you have written a specific handler in your application for this exception, and are able to handle it more gracefully).