我是否打印错误信息?取决于环境。如果程序是 将用于自动或嵌入式系统,那么我不会打印 错误信息(没有人可以看到它)。如果应用程序是 总是以交互方式运行,那么我会打印一条错误消息。 在图形环境中打印错误消息可能很困难。 If malloc fails what should I do? 1. Exit imediately.Maybe. 2. Print an error message (or put a log entry) and exit.Maybe. 3. Print an error message (or put a log entry) and continue execution (after possibly recovering from the error).Maybe.The answer all depends on the situation. If IO absolutely must have asuccessful malloc or there is not point continuing then I would exit. IfI''m attempt to allocate a large block of memory but can get by with asmaller block then adjust and keep going.Do I print an error message? Depends on the environment. If the program isgoing to be used in an automated or embedded system then I would not printan error message (there will be no human to see it). If the application isalways going to be run interactively then I would print an error message. Printing an error message might be difficult in a graphical environment. 为什么?如果只是因为你必须完成创建 对话框的工作,那么学会创建一个对话框。如果是因为失败的malloc 意味着没有足够的内存来显示错误对话框,请查看是否有是打印错误消息的系统级方式。操作系统通常会为打印关键错误对话框保留内存。如果您的操作系统没有这样做,请在开始时分配足够的内存,以便有内存来打开错误对话框。保留该内存,直到您退出 申请。 - 发送电子邮件至:darrell at cs dot toronto dot edu 不要发送电子邮件至 vi ** **********@whitehouse.gov HA Sujith写道:H.A. Sujith wrote:如果malloc失败我该怎么办? 1.立即退出。 2.打印错误信息(或输入日志条目)和退出。 3.打印错误信息(或输入日志条目)并继续执行执行(可能从错误中恢复)。 打印错误信息可能很困难在图形环境中。 If malloc fails what should I do? 1. Exit imediately. 2. Print an error message (or put a log entry) and exit. 3. Print an error message (or put a log entry) and continue execution (after possibly recovering from the error). Printing an error message might be difficult in a graphical environment. 我已经走下了试图让malloc()失败的途径,而且它更容易了b / b 说完了。此外,实现特定设置必须设置为以创建malloc()失败的可能性。 (例如,RLIMIT_DATA linux中的,BSD中的malloc.conf等)。 一个很好的练习就是尝试将malloc()转换为在您的 实施下失败,以确保您列出的上述项目均有效。并且 不要忘记为尾随的''\ 0'分配足够的空间。 我最近转移到openBSD试验制作malloc() 失败。它确实是特定于实现的,所以我很好奇 常规在这里考虑在平台之间移植malloc()因为那里没有似乎没有 malloc()失败时,是否设置了errno的标准。 brianI have gone down the avenue of trying to make malloc() fail, and it''seasier said then done. Also, implementation specific settings have tobe set to create a possibility of malloc() failing. (e.g., RLIMIT_DATAin linux, malloc.conf in BSD, etc.).A good exercise would be just trying to get malloc() to fail under yourimplementation to make sure the above items you listed even work. Anddo not forget to allocate enough space for the trailing ''\0''.I have recently moved over to openBSD to experiment with making malloc()fail. It''s really implementation specific, so I''m curious what theregulars here think about porting malloc() between platforms since theredoesn''t seem to be a standard as to whether or not errno is set whenmalloc() fails.brian 这篇关于如果malloc失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-30 05:36