You are given the source to an application which crashes when it is run. After running
it ten times in a debugger, you find it never crashes in the same place. The application
is single threaded, and uses only the C standard library. What programming errors
could be causing this crash? How would you test each one?
大意:你得到一个应用的源码,该应用每次启动都会出现程序崩溃。再你调试了10次后发现,在一个相同的条件下,该程序从不崩溃。
该程序是单线程,并且使用的库函数是c的标准库。那么,该程序崩溃的原因是什么?你怎样检测它呢?
The question largely depends on the type of application being diagnosed. However, we can
give some general causes of random crashes.1. Random variable: The application uses some random number or variable component
which may not be fixed for every execution of the program. Examples include: user
input, a random number generated by the program, or the time of day.
2. Memory Leak: The program may have run out of memory. Other culprits are totally
random for each run since it depends on the number of processes running at that
particular time. This also includes heap overflow or corruption of data on the stack.
It is also possible that the program depends on another application / external module that
could lead to the crash. If our application, for example, depends on some system attributes
and they are modified by another program, then this interference may lead to a crash. Pro-
grams which interact with hardware are more prone to these errors.
In an interview, we should ask about which kind of application is being run. This information
may give you some idea about the kind of error the interviewer is looking for. For example,
a web server is more prone to memory leakage, whereas a program that runs close to the
system level is more prone to crashes due to system dependencies.
答案:该问题很大程度上取决于对该应用的诊断。但我们可以总结一下一些随机常见的崩溃原因:
1. 不确定的变量:程序使用了随机的数字或变量组件,在程序执行时没有被很好的确定范围。如:用户输入,随机数,或时间。
2.内存泄漏:程序每次执行时都内存溢出。每次运行程序崩溃的罪魁祸首是随机的,因为它依赖于在特定时间运行的进程数。
包括堆溢出或者是栈异常。
程序依赖于其它外部应用也可能导致崩溃。如果我们的应用依赖于一个系统某些系统属性,他们被另一个程序修改,那么这个干扰可能会导致系统崩溃。与硬件交互的程序更容易出现这些错误。例如,一个Web服务器是更容易出现内存泄漏,而接近系统级运行的程序更容易崩溃,由于系统的依赖。