C嵌入式错误处理的优化方式

发布于 2024-12-02 15:06:11 字数 950 浏览 0 评论 0原文

基本问题,但想知道优化的方法。它是用于关闭多个进程的关闭例程的代码块。在所有情况下,它应该尝试关闭所有进程和阶段,但保留失败值以返回。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤云独去闲 2024-12-09 15:06:11

您需要为链接列表或数组中的每个错误代码动态分配空间,然后将列表传递回需要知道结果的任何地方。

您可以为每个正在关闭的进程提供一个代码,并在其中包含“正常”情况,也可以将进程 ID 和错误代码一起包含在内,并且仅在出现错误时才包含它们。

例如:(

errorList = NULL;
for i 1 to 10
 if((result = operation())!=PASS) {
  appendToList(errorList, i, result)
 }
if(errorList != NULL) {
 result = ERROR;
} else {
 result = PASS;
}
return result;

注意:虽然您说过您是用 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:

errorList = NULL;
for i 1 to 10
 if((result = operation())!=PASS) {
  appendToList(errorList, i, result)
 }
if(errorList != NULL) {
 result = ERROR;
} else {
 result = PASS;
}
return result;

(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).

无力看清 2024-12-09 15:06:11

如果您需要知道的是您的迭代是否失败,您可以使用 ++ 运算符:

int result = 0;

for i 1 to 10
if(operation() != PASS)
    PRINT "FAILURE"
    result++;
done    

return (result == 0 ? PASS : ERROR);

If all you need to know is whether any of your iterations failed, you can use the ++ operator:

int result = 0;

for i 1 to 10
if(operation() != PASS)
    PRINT "FAILURE"
    result++;
done    

return (result == 0 ? PASS : ERROR);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文