-2

Can't resist to ask after seeing answers to my earlier question.

char* msg = "Stack-overflow"; // Stored on read-only memory region of process.
int i=10; // Stored onto stack.

why "Stack-overflow" will be stored on read-only memory area of process and value 10 is not?

Edited to clear my question.

Community
  • 1
  • 1
VishalDevgire
  • 4,232
  • 10
  • 33
  • 59

3 Answers3

2

Neither of the variables are stored in read-only memory. However, the memory msg points to is read-only.

That's not enforced by the C standard though - it's simply common practice to do so. There is more information about this in the following question: String literals: Where do they go?

Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • I believe the follow-up would be why is the string `Stack-overflow` stored in read-only memory. +1 for your distinction. – RageD Apr 15 '13 at 18:11
0

You should compare the string "Stack-overflow" with the 10 and the variable msg with the variable i. You are allowed to change the pointer value of msg and you are not allowed to change the value of 10. If the 10 would be a bigger difficult number there is a good chance it ends up in read-only memory too.

Bryan Olivier
  • 5,207
  • 2
  • 16
  • 18
0

No variable is stored in read-only memory, msg points to read-only memory so if we try to modify it compiler will flag an error. although we can assign msg to other address.(It behaves like pointer to constant object in c++)

anshul garg
  • 473
  • 3
  • 7