C嵌入式错误处理的优化方式
基本问题,但想知道优化的方法。它是用于关闭多个进程的关闭例程的代码块。在所有情况下,它应该尝试关闭所有进程和阶段,但保留失败值以返回。
Error_code result=PASS;
<some code>
for i 1 to 10
if((result = operation())!=PASS)
PRINT "FAILURE"
done
if((result = operation())!=PASS)
PRINT "FAILURE"
for i 1 to 10
if((result = operation())!=PASS)
PRINT "FAILURE"
done
return result;
现在的问题是,如果最后一次迭代是通过,那么它返回一个通过,因为该操作应该运行所有 10 次迭代,并且如果失败,我们只需要返回错误代码。因为它实际上可能有 100 次失败,所以我这样编写代码:-
Error_code result=PASS;
Error_code tresult=PASS;
for i 1 to 10
if((result = operation())!=PASS) {
tresult = result;
PRINT "FAILURE"
}
if((result = operation())!=PASS) {
tresult = result;
PRINT "FAILURE"
}
for i 1 to 10
if((result = operation())!=PASS)
tresult = result;
PRINT "FAILURE"
done
if(tresult != result)
result = tresult;
return result;
这是唯一优化的解决方案,还是我们可以在这方面做得更好。我无法使用 flag,因为它有超过 100 个错误变量。 有什么更好的办法推荐一下..
Basic question but wanted to know optimized way of doing it.. It is a code block for a close routine having multiple process to close. In all the cases, it should try to close all the processes and stages, but keep the failures value to return back.
Error_code result=PASS;
<some code>
for i 1 to 10
if((result = operation())!=PASS)
PRINT "FAILURE"
done
if((result = operation())!=PASS)
PRINT "FAILURE"
for i 1 to 10
if((result = operation())!=PASS)
PRINT "FAILURE"
done
return result;
now the issue is if last iteration is Pass, then it return a PASS, Since the operation should be running for all 10 iterations, and in case of failure, it is just that we need to return the Error code. Since it can virtually have 100 failures, I write the code like this:-
Error_code result=PASS;
Error_code tresult=PASS;
for i 1 to 10
if((result = operation())!=PASS) {
tresult = result;
PRINT "FAILURE"
}
if((result = operation())!=PASS) {
tresult = result;
PRINT "FAILURE"
}
for i 1 to 10
if((result = operation())!=PASS)
tresult = result;
PRINT "FAILURE"
done
if(tresult != result)
result = tresult;
return result;
Is it the only optimized solution, or we can do better in this. I can't use flag, as it has more than 100 error variables..
Suggest any better way..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要为链接列表或数组中的每个错误代码动态分配空间,然后将列表传递回需要知道结果的任何地方。
您可以为每个正在关闭的进程提供一个代码,并在其中包含“正常”情况,也可以将进程 ID 和错误代码一起包含在内,并且仅在出现错误时才包含它们。
例如:(
注意:虽然您说过您是用 C 编写的,但您的代码片段看起来更像伪代码,所以这就是我在这里解决它的方式。您需要编写适当的代码来附加错误代码到您的列表)。
You need to dynamically allocate space for each error code in a linked list or array and then pass the list back to whatever needs to know the results.
You could either have a code for each process being closed and include the "OK" cases in there, or you could include the process ID and the error code together and only include them when there is an error.
For example:
(Note: Although you've said you're writing in C, your code snippet looks more like pseudo code, so that's how I've addressed it here. You'd need to write the appropriate code to append the error code to your list).
如果您需要知道的是您的迭代是否失败,您可以使用
++
运算符:If all you need to know is whether any of your iterations failed, you can use the
++
operator: