c - What happens by modifying read only memory? -
is identifier qualified const in every case stored read only? or determined on run time? , and/or happen when i'm going write "read only" memory.
const char **cpp; char *p; const char c = 'a'; cpp = &p; *cpp = &c; *p = 0; is code going modify ro mem in case? or did work because every time executed it wasn't ro mem? if so, happen if char stored in ro mem , i'm executing code? behaviour undefined that's not question. code executable. question is: happen modifying read memory?
basically, depends on compiler. char on stack , not readonly (so, const mean mess type conversions, nothing in executable). stack never readonly.
if define const global, data segment not readonly either. again working. though can imagine compiler putting consts special segment readonly access, in case usual access violation/segfault.
also, may have clever compiler optimize entire code , replace occurences of const variable name value, , in case nothing can told modifying variable. well, most probably compiler clever enough see &c understand variable must stored somewhere.
note multiple most probably , maybe. undefined behavior undefined behavior, answer read we don't know happen.
Comments
Post a Comment