此问题涉及遵循 MISRAC:2012 指南的 ISO C99 编码。
\n\n我正在寻找有关 Dir 4.8 \xe2\x80\x9c 的指导如果指向结构或联合的指针从未在翻译单元内取消引用,则该对象的实现应与 Dir 4.12 \ 一起隐藏\xe2\x80\x9d xe2\x80\x9c不应使用动态内存分配\xe2\x80\x9d。
\n\n在 C 中实现抽象数据类型时,通常使用句柄来引用 ADT,该句柄是指向描述 ADT 内部状态的结构的指针。这可以根据 Dir 4.8 使用不透明指针来完成,其优点是内部细节对用户保持隐藏。
\n\n通常,可能存在多个 ADT,因此必须有一种方法来创建多个句柄。这可以通过在初始化函数中为句柄引用的内部细节分配内存来解决,但是,这在 Dir 4.12 下是不允许的。
\n\n另一种选择是初始化例程接收用户提供的指向静态分配句柄的指针,但是,这不能使用不透明指针来完成。
\n\n我在下面说明这个问题。
\n\n Module.h \n\n struct module; \n typedef struct module module_t; /* Module handle is only available to the world as an incomplete type. This allows us to satisfy MISRAC 2012 Dir 4.8.*/\n\n Module.c\n\n #include "module.h"\n struct module\n {\n uint8_t value;\n };\n module_t* module_get_a_handle(void)\n …Run Code Online (Sandbox Code Playgroud) 我试图从我的代码中删除规则11.3.
示例代码:
static int32_t
do_test(const char *cp)
{
const char *c = cp;
const int32_t *x;
x = (const int32_t *)cp;
return *x;
}
Run Code Online (Sandbox Code Playgroud)
我希望*c和*x的值相同.即使代码正在编译并给出正确的值,"x =(int32_t*)cp;" 导致违反11.3和11.8
规则11.3违规:具有指针类型的对象不应转换为指向不同对象类型的指针.
我已尝试使用void指针,但结果与我的预期不一样,并且还导致了额外的违规.
反正有没有删除这些违规行为?
从MISRA C 2012文档中可以看出,这个规则有一个例外,因为它允许将指向对象类型的指针转换为指向对象类型char,signed char或unsigned char之一的指针.标准保证可以使用指向这些类型的指针来访问对象的各个字节.
由于char类型而忽略Dir 4.6.
面临与违反 MISRA C 2012 规则 11.4 相关的问题。使用 PC-Lint Plus 进行规则检查。Keil uVision V5.38.0.0
错误:
conversion between object pointer type 'GPIO_Type *' and integer type 'unsigned int' [MISRA 2012 Rule 11.4, advisory]```
uint *L_uc_byte = (uint *)&GPIO->PIN[0];
Run Code Online (Sandbox Code Playgroud)
以下是GPIO相关的详细信息
#define __IO volatile /*!< Defines 'read / write' permissions */
/** Peripheral GPIO base address */
#define GPIO_BASE (0x4008C000u)
/** Peripheral GPIO base pointer */
#define GPIO ((GPIO_Type *)GPIO_BASE)
/** GPIO - Register Layout Typedef */
typedef struct {
__IO uint8_t B[6][32]; /**< Byte …Run Code Online (Sandbox Code Playgroud)