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.
There's really no formal definition. But broadly speaking, a crash occur when an uncontrolled error happens. Freeze is when an application stops to respond to any event (for example an infinite loop) but no actual error happens (no exception thrown).
When a program crashes, something unexpected has happened which the program itself is not equipped to handle; when the operating system detects such an event, it (usually) terminates the program.
Hangs have varied causes and symptoms, including software or hardware defects, such as an infinite loop or long-running uninterruptible computation, resource exhaustion (thrashing), under-performing hardware (throttling), external events such as a slow computer network, misconfiguration, and compatibility problems.
What Does Crash Mean? A crash, in the context of computing, is an event wherein the operating system or a computer application stops functioning properly. It mostly occurs when: Hardware has failed in a non-recoverable fashion. Operating system data have become corrupted.
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
@Pop, Kristo: Am actually checking on a code that hangs but I see some problems on memory leak. But I'm not really sure if memory leak causes a program to hang. – jasonline
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 new
operator fails to allocate memory, a std::bad_alloc
exception 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).
Hangs can also be caused by waiting for external resources, mostly networking. Though that usually times out after a while. A hang may also be caused by the termination of a thread that handles something related to processing. For example, if a UI thread dispatched a worker thread to do some work and the worker thread died, the program would appear to be hung.
A lot of the times Windows apps hang because something happens to their message loop processing. Since all of the program events come trough the message loop once that is compromised, the program becomes unresponsive.
You can read more about how message loop works here:
http://www.winprog.org/tutorial/message_loop.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With