我在一些安全关键汽车模块中运行代码。下面是代码的一个粗略估计:丢失挥发性限定
在下面的代码是一个模块的一部分 - “主模块”,拥有该易失性可变/数组“x_ast”
Main Module.c
//The structure x contains the major data that needs to be stored in case of a crash event. i.e. a real car crash
// x contains data such a vehicle speed, environment data, sensor data, CAN related data,etc. Basically x itself has lot of structures inside it.
typedef struct x x_tst;
volatile x_tst x_ast[5];
//x_ast is being used in realtime and background functions, considering proper interrupt disabling and enabling.
在下面的代码是模块的一部分 - “DependentModule”,可以共享缓冲区“x_ast”。
DependentModule.c
extern volatile x_tst x_ast[5];
//x_ast is owned by a separate module
//Now I need to share this buffer "x_ast" to a different module that will inturn fill data in it for some conditions. Say for a particular condition that is activated, the buffer "x_ast" will be shared across to a module "Conditional Data Record".
//The base address of first indexed buffer "x_ast[1]" is provided to "Conditional Data Record" for access.
//The main module will not access the buffer that is shared, once the "Conditional Data Record" is activated/requested.
// Below is a mechanism to share the buffer "x_ast" to module "Conditional Data Record".
// This API will be called by module - "Conditional Data Record" once it is activated. It is ensured that this takes place only during intialization of whole system, and no other time.
boolean GetConditionalBuffer(uint8 **PtrToBuffer)
{
boolean BufferShared = FALSE;
void* Temp_ptr;
*PtrToBuffer = NULL;
// if module "Conditional Data Record" is activated? then share the buffer "x_ast", else do not share the buffer.
if(Conditional Data Record == activated) {
Temp_ptr = (x_tst*)&x_ast[1];
*PtrToBuffer = Temp_ptr;
BufferShared = TRUE;
}
return BufferShared;
}
Referring to the line of code:
Temp_ptr = (x_tst*)&x_ast[1];
的代码(Tempptr =(x_tst *)& x_ast [1])的上面的行抛出一个警告“消息(7:0312)。危险指针铸造导致挥发性资格的损失 “ 上述警告是强制性警告,因此有必要解决它。
我知道我将一个volatile变量的地址赋值给一个void指针,导致volatile限定的丢失。 我尝试了不同的方法试图解决警告,但无法得到确凿的方法。
有没有什么办法,我可以修改代码并删除这个警告,或者可以绕过这个警告。
而不是使用'volatile void ** PtrToBuffer'函数。另一种选择是在不同的缓冲区中完成您的工作,并根据需要将其复制到易失性缓冲区中。后者风险较小。请记住,您将需要锁定缓冲区以防止您自己并发访问以及其中一个后台功能。 –
在“条件数据记录”模块中处理并发条件。确保避免比赛条件。 –
如果你的意思是该函数锁定了缓冲区(并且稍后你将要解锁它),那么使该函数采用'volatile void ** PtrToBuffer'。 (不易挥发uint8_t)。抛出volatile和write会导致未定义的行为,即使你锁定了缓冲区。您需要使用赋值运算符一次读取和写入缓冲区1的值。 –