我在一些安全關鍵汽車模塊中運行代碼。下面是代碼的一個粗略估計:丟失揮發性限定
在下面的代碼是一個模塊的一部分 - 「主模塊」,擁有該易失性可變/數組「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的值。 –