gpt4 book ai didi

c++ - 从原始内存中读取值(符合 MISRA)

转载 作者:行者123 更新时间:2023-11-30 04:01:28 25 4
gpt4 key购买 nike

我正在尝试读取之前写入 NVM 闪存的变量的值。

我的代码是:

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 转换为指针。有谁知道访问此内存的方法吗?

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

如果不打破它们,规则是什么? :)

最佳答案

如我所见。对于这个问题,我只有 2 个“解决方案”:

1.- 不符合 MISRA。

2.- 在动态环境中使用静态地址:

编译时:

const Table1 table1 __attribute__ ((section (".table1space")));
const Table2 table2 __attribute__ ((section (".table2space")));

在链接描述文件中定义所需的部分。

在运行时:

当对table1 调用动态分配时。返回静态table1地址等。

关于c++ - 从原始内存中读取值(符合 MISRA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25720358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com