I have written a small method that is supposed to allow me to easily overwrite the value of a static constant variable.
Here is the variable I want to change:
static const unsigned int myInt;
Here is the method that I am trying to use:
template<typename T>
void MyClass::SetConstantVariableValue(void* destination, T& value)
{
memcpy(destination, (const void*)&value, sizeof(value));
}
And here is the statement used to call the method:
int a = 5;
this->SetConstantVariableValue((void*)&myInt, a);
My problem is that it works perfectly if myInt is only static and not constant. As soon as I define it as constant, memcpy crashes from an access violation.
Unhandled exception at 0x596EEB90 (msvcr110d.dll)
0xC0000005: Access violation writing location 0x00AC16B8.
It is my understanding that memcpy ignores the fact that a variable might be constant since it is performed at runtime and has no idea what the datatype is for either the destination or the source. If that is correct, then the combination of being static and const is what is causing the crash. I have not been able to find anything explaining as to why this might cause memcpy to crash. Anyone know what I am doing wrong?