1 goto关键字
1.1 goto关键字分析
根据项目经验来看,程序质量与goto的出现次数成反比。所以,一般有高手潜规则:禁用goto。我们对于goto最后的判决:就是将goto打入冷宫,不再使用!
1.2 goto副作用分析
代码如下:
#include <stdio.h>
#include <malloc.h>
void func(int n)
{
int* p = NULL;
if( n < 0 )
{
goto STATUS;
}
p = (int*)malloc(sizeof(int) * n);
STATUS:
p[0] = n;
free(p);
}
int main()
{
printf("begin...\n");
printf("func(1)\n");
func(1);
printf("func(-1)\n");
func(-1); // 段错误
printf("end...\n");
return 0;
}
参考资料:
本文深入探讨了C语言中goto关键字的使用及其潜在副作用,通过具体代码示例展示了goto可能引发的问题,并强调了现代编程实践中避免使用goto的重要性。
3570

被折叠的 条评论
为什么被折叠?



