From: K. F. <kfr...@gm...> - 2013-02-09 01:01:00
|
Hi woyt.a! On Fri, Feb 8, 2013 at 6:30 PM, <wo...@wp...> wrote: > Hi, > > following code: > ... > static const size_t val1 = 0; > > // causes access violation errof on runtime > size_t* pVal1 = const_cast<size_t*>(&val1); > *pVal1 = 7; > ... > > can change value of val0 const variable but invokes segmentation fault for val1. > This is known issue, as val1 is put in the read-only memory page. > > My question is: > 1. is there any way to change static cont variable value? I'm not certain of this, but I believe that the standard does permit val1 to be read-only. If so, then there is no portable way to change a static const variable. I don't believe that the standard requires static const to be forced to be read-only (but I could be wrong). If so, a compiler might let you change it, or maybe there's a compiler setting that would let you change it. (But I would expect not.) Perhaps someone more knowledgeable about the standard could give us a more authoritative (and correct) answer. > 2. is there any way to prevent compller to put that variable in read-only memory? Not that I'm aware of, but again, maybe a specific compiler offers some kind of setting to control this behavior. If I were doing this, I would not declare the variable const. (Or not declare it static, etc.) (If this is a real use case you're facing, I'm sure we could figure out an adequate alternative.) > Regards, > woyt.a Good luck. K. Frank |