Skip to content
Advertisement

C – How can a pointer overwrite a local const block of memory but not the global const block of memory?

Last time I asked how can I generate segmentation fault in a program by bypassing the compiler’s promise of not overwriting the const memory. User Marco Bonelli described the following way, which worked perfectly.

JavaScript

Either way I was able to generate segmentation fault.

  • JavaScript
  • JavaScript

Now my question is what is it that is preventing the pointer from writing to a global const block of memory but not to the local const block of memory. For example, in the below code I was able to write to the const block of memory without any issue.

JavaScript

I’m curious about knowing how this happened. Please enlighten me.

If it matters, I’m using gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0.

Advertisement

Answer

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement