Tool/software: TI C/C++ Compiler
Hello,
I'm writing today to ask how I should best handle a situation where my code is causing a memory protection fault because it is dereferencing a null pointer. Here is the simple pseudo-code of my function:
value = 0;
if (guardFlagIsSet)
{
// Perform operation that dereferences a pointer
value = *pointer;
}
return value;
The guard flag is not set until the pointer is initialized, but this function is called both before and after that happens during the life of my system. What I am seeing is that due to compiler optimization, the assembly is dereferencing the pointer and reading the memory "at the same time" that it is evaluating the guard flag. If the guard flag ends up being false, then the value read from memory is ultimately not used. So the behavior is certainly correct, but I end up with a memory protection fault on the DSP because the uninitialized pointer is read.
My guess is that there are a few different ways to solve this problem; perhaps by making the guard flag volatile (although I'm not sure if that is guaranteed to work), perhaps with a pragma. I'd like to ask for a recommendation from someone who is very familiar with the TI compiler. I want to make sure that the solution is relatively future-proof, so that when I upgrade compiler versions in the future it is not likely to return. Thanks in advance!
Best regards,
Dave