我正在寻找有关在C文件中记录退出代码的说明。
例如,我有以下内容:
if( !(new_std = (student*) malloc(sizeof(student))) )
exit(1);
//+1 is for the \0, strlen does not give us that one!
if( !(new_std->name=(char*) malloc(1+sizeof(char)*strlen(name)))){
free(new_std);
exit(1);
}
在文件中以数字1退出(即内存分配失败)的文档的正确记录方式是什么?
没有“正确答案”,但是我想大多数人都会建议使用常量:将它们放在任何C文件都可以包含的通用头文件中。
exit_codes.h
#define EXIT_SUCCESS 0
#define EXIT_GENERAL_FAILURE 1
#define EXIT_OUT_OF_MEM 2
随便什么
#include "exit_codes.h"
void *p = malloc(100);
if (!p)
exit(EXIT_OUT_OF_MEM);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句