c - Can we change the value of an object defined with const through pointers? -
#include <stdio.h> int main() { const int = 12; int *p; p = &a; *p = 70; }
will work?
it's "undefined behavior," meaning based on standard can't predict happen when try this. may different things depending on particular machine, compiler, , state of program.
in case, happen answer "yes." variable, const or not, location in memory, , can break rules of constness , overwrite it. (of course cause severe bug if other part of program depending on const data being constant!)
however in cases -- typically const static
data -- compiler may put such variables in read-only region of memory. msvc, example, puts const static ints in .text segment of executable, means operating system throw protection fault if try write it, , program crash.
in other combination of compiler , machine, entirely different may happen. 1 thing can predict sure pattern annoy whoever has read code.
Comments
Post a Comment