主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

MISRA C:2012 Rule 17.8

A function parameter should not be modified

描述

规则定义

A function parameter should not be modified 1 .

理由

当您修改一个参数时,与该参数对应的函数参量不会被修改。但是,您或其他不熟悉 C 语言的程序员可能会错误地认为,当您修改参数时,参量也会被修改。

Polyspace 实现

当您在函数体中修改函数参数时,Polyspace® 会报告违反此规则。当函数参数作为 const 通过地址传递给另一个函数时,Polyspace 不会报告违反此规则。

故障排除

如果您预期会出现违规,但未看到该违规,请参阅诊断为何编码规范违规未按预期显示

示例

全部展开

在此示例中,当参数 param1 被修改时,规则被违反。Polyspace 报告了违规情况。因为 param3 被作为 const 传递给 foo,因此它不会被修改。Polyspace 不会报告违规情况。

int input(void);
int foo(const int*);
void func(int param1, int* param2, int param3) {
    
    param1 = input();   /* Non-compliant */
    *param2 = input();  /* Compliant */
    foo(&param3); /*Compliant*/
}

当修改指针参数(如 param2)时,该规则不被违反。

检查信息

组:函数
类别:建议
AGC 类别:可读性

版本历史记录

在 R2015b 中推出

全部展开


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.