I heard that in C, if I do
char *s = "hello world".
the "hello world" is actually stored in read-only memory.
I am not so clear about read-only memory. What is the explanation? Is that like a flag to the compiler that tells the compiler to do not write into that section?
That's not a feature of the C language but a feature of the compiler/linker and the operating system working together.
When you compile such code the following happens:
The compiler will put the string into a read-only data-section.
The linker collects all the data in such read-only sections and puts them into a single segment. This segment resides in the executable file and is flagged with a "read only"-attribute.
Now comes the operating system executable loader. It loads the executable (or maps it into memory to be more exact). Once this is done, the loader walks the sections and sets access-permissions for each segment. For a read-only data segment it will most likely disable code-execute and write access. Code (for example, your functions) gets execute rights but no write access. Ordinary data like static variables gets read and write access and so on...
That's how modern operating systems do it.
As said, it's not a feature of the C language. If you compile the same problem for DOS, for example, the program will run but no write protection would be possible, because the DOS-loader does not know about read-only sections.
Executables contain two parts: a .data section, containing global variables, and a .text section, containing the actual machine code.
Strings are placed into the .data section. What C does when it sees "Hello world" is it puts the string "Hello world" into the executable itself, and replaces instance of "Hello world" in the program with the address where that string ends up being loaded.
Having said that, I'm not sure why it's read-only - theoretically a program should be able to modify its own memory..
True read-only memory is implemented by the memory subsystem of the OS. The OS can mark certain pages as read-only.
In the binary, the compiler can tell the OS which parts of the executable should be placed in read-only vs read-write memory pages.
When you write
char s="sneha"; you are allocating 10 bytes of storage space(not memory, memory comes into picture only when u r executing your program) in your object file. This is static allocation of memory( at compile time).
But when you write
char *s="sneha"; you are not allocating any storage space to store
"sneha". It will get stored in READ ONLY section. But the pointer
s is stored in different section based on where it is declared. But it is pointing to the READ ONLY DATA
"sneha". So if you try to write on it you will get segmentation fault.
char *s = "sneha"; s = 'N'; printf("%s",s); // you expecting output sNeha, // but you get a seg fault since it is READ ONLY DATA
As other folks have mentioned, whether the contents of constant strings are stored in read-only memory is determined by the operating system, compiler, and chip architecture.
More precisely, the C standard specifies that the quoted strings are considered to have "const char" type (or words to that effect, I don't have the standard at hand).
Any code that attempts to modify the contents of such a string is invoking undefined behavior. That means that literally anything can happen at that point, and the provider of the compiler isn't even required to document what can happen.
In practice, this means that a C or C++ program that wants to be portable has to avoid modifying constant strings.
In general, the compiler will not allow you to modify the contents of of "const" variables, so you can consider "const" to mean "read only" in most cases. Unfortunately, there's a special exception for char * and const char *, largely for historical reasons. That means that code like this:
char *x = "Hello, World"; *x = 'h';
will compile without error or warning, even though it invokes undefined behavior.
You could try something like
s = '0';
and see if it says "hello w0rld" when you call
If it causes an immediate Segmentation Fault or a Data Execution Prevention exception then it is probably read only. (If the system lets you get away with it, that doesn't make it a good idea.)