从原始内存读取值(符合MISRA) [英] Reading a value from raw memory (MISRA compliant)

查看:151
本文介绍了从原始内存读取值(符合MISRA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取以前写在NVM闪存上的变量的值.

I'm trying to read the value of a variable previously write on NVM flash.

我的代码是:

uintptr_t address = getAddress();
//[MISRA C++ Rule 5-2-8] cast from unsigned int to pointer
uint16_t value = *(reinterpret_cast<uint16_t*>(address)); 

问题是在MISRA中不允许从uintptr_t强制转换为指针.有人知道访问此内存的方法吗?

The problem is the cast from uintptr_t to pointer is not allowed in MISRA. Do anyone knows a way to access this memory?

我违反了MISRA的一项重要规定.使用动态内存(闪存的内容是动态的,因此数据的地址是可变的).仅当您考虑声明一个指向闪存地址的const指针并在写入数据后对其进行访问时,才会引起这种情况.

I'm breaking one of the big rules of MISRA. Using dynamic memory (the contents of flash is dynamic so the address of data is variable). Only cause if you're thinking of declare a const pointer to the flash address and access it after the data is write.

如果不作废,有什么规则? :)

What are the rules for if not for break them? :)

推荐答案

MISRA C ++(必需)规则5-2-8试图阻止您执行操作,从而导致不确定的行为.等效的MISRA C:2012准则是(咨询性)规则11.4

MISRA C++ (Required) Rule 5-2-8 is attempting to stop you doing things, which lead to unspecified behaviour. The equivalent MISRA C:2012 guideline is (Advisory) Rule 11.4

并且通常不希望将整数转换为指针,因为存在许多可能的问题,尤其是与对齐有关.

And in general converting an integer to a pointer is undesirable, as there are a variety of possible problems, not least to do with alignment.

MISRA C准则有一些额外的叙述

The MISRA C guideline has some extra narrative

在可能的情况下,应避免在指针和整数类型之间进行转换,但是在寻址存储器映射的寄存器或其他特定于硬件的功能时可能有必要.

Casting between a pointer and an integer type should be avoided where possible, but may be necessary when addressing memory mapped registers or other hardware specific features.

处理此问题的 正确 方法是引起偏差.这记录了您需要这样做的原因 为什么 ,但也使您 考虑 后果,以及 显示 表明您正在缓解它们.

The correct method for dealing with this is to raise a deviation. This documents the reason why you need to do it, but also makes you consider the consequences, and to show that you are mitigating them.

这篇关于从原始内存读取值(符合MISRA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆