I have a struct declared and set in memory, I have a global constant pointer to this struct and throughout my program I dereference this pointer to access the different parts of the struct. However, there are times when the pointer memory address changes when dereferenced from a specific function.
My struct
typedef struct configData_t
{
uint8_t version[4];
inputConfig_t inputModuleConfig [MAX_INPUT];
outputConfig_t outputModuleConfig [MAX_OUTPUT];
notificationConfig_t notificationConfig [MAX_NOTIFICATIONS];
functionConfig_t autoFunctionConfig [MAX_FUNCTIONS];
uint16_t Crc16;
} configData_t;
The constant pointer is declared by setting the memory address of the data (externally loaded and outside of the applications memory)
//Pointer points to memory location on uC (data already in memory)
const configData_t* theConfigData = (configData_t*)0x0460000;
To get a notification from the 'notificationConfig' array I dereference 'theConfigData' by [1]:
const notificationConfig_t *pNotificationConfig = theConfigData->notificationConfig + notificationID;
The following occurs when stepping through the code on the uC:
Function B does not alter 'theConfigData' in any way. In the debuggers memory view, the data in 0x0460000 + sizeOf(configData_t) is not altered in any way.
How is the 'pNotificationConfig' pointer changing address when going from function A to B?
You need to make sure that :
configData_t
is exactly the same in both the function A and function B compilation unitsconfigData_t
is exactly the same for both the function A and function B compilation unitsRed flags of the above for your specific issue would be eg. :
sizeof(configData_t)
is differentoffsetof(configData_t, notificationConfig)
is differentsizeof(notificationConfig_t)
is differentIf one or more of these red flags are raised (and in a comment, you confirm that), you need to determine which of the two earlier options causes it :
MAX_INPUT
, MAX_OUTPUT
, ... in your case)