【问题标题】:MISRA Violation Rule 15.5 : Multiple points of exit detected. Function should have a single point of exit at the end of the function [duplicate]MISRA 违反规则 15.5:检测到多个出口点。函数应该在函数末尾有一个退出点[重复]
【发布时间】:2018-05-06 22:50:51
【问题描述】:

我正在尝试从我的代码中删除规则 15.5。这基本上是因为函数中有多个返回。

代码如下:

int32_t
do_test(int32_t array[])
{  
    for(int32_t i=0; i < VAL; i++)
    {
      if(array[i] == 2) {
        return 1;
      } 
    }
    return 0;
}

我尝试了一个临时变量,它存储返回值并在最后返回这个变量。但这没有用。

有什么建议吗?

【问题讨论】:

  • 您需要使用一个临时变量来存储返回值并在最后返回这个变量。因为那确实有效。如果你没有做错。
  • 你能告诉我们你失败的临时变量尝试吗?
  • int32_t do_test(const int8_t array[]) { int32_t temp; for(int32_t i=0; i
  • 这条规则不会自动提高程序的可靠性或可读性。
  • @JonathanLeffler 大多数 misra 规则使代码变得更糟 IMO

标签: c misra


【解决方案1】:

你需要存储一个临时变量来打破循环

int32_t
do_test(int32_t array[])
{
    int32_t result = 0;  
    for(int32_t i=0; i < VAL; i++)
    {
      if(array[i] == 2) {
        result = 1;
        break; // !!
      } 
    }
    return result;
}

【讨论】:

  • 是的。工作正常。谢谢!!!
猜你喜欢
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 2023-01-03
  • 1970-01-01
  • 2021-08-13
  • 2019-04-30
  • 1970-01-01
相关资源
最近更新 更多