2
votes

I read this on wikipedia

    int main(void)
 {

    char *s = "hello world";
    *s = 'H';

 }

When the program containing this code is compiled, the string "hello world" is placed in the section of the program executable file marked as read-only; when loaded, the operating system places it with other strings and constant data in a read-only segment of memory. When executed, a variable, s, is set to point to the string's location, and an attempt is made to write an H character through the variable into the memory, causing a segmentation fault**

i don't know why the string is placed in read only segment.please someone could explain this.

5

5 Answers

3
votes

String literals are stored in read-only memory, that's just how it works. Your code uses a pointer initialized to point at the memory where a string literal is stored, and thus you can't validly modify that memory.

To get a string in modifiable memory, do this:

char s[] = "hello world";

then you're fine, since now you're just using the constant string to initialize a non-constant array.

3
votes

There is a big difference between:

char * s = "Hello world";

and

char s[] = "Hello world";

In the first case, s is a pointer to something that you can't change. It's stored in read-only memory (typically, in the code section of your application).

In the latter case, you allocate an array in read-write memory (typically plain RAM), that you can modify.

2
votes
  • When you do: char *s = "hello world"; then s is a pointer that points to a memory that is in the code part, so you can't change it.

  • When you do: char s[] = "Hello World"; then s is an array of chars that are on the stack, so you can change it.

If you don't want the string to be changed during the program, it is better to do: char const *s = ....;. Then, when you try to change the string, your program will not crash with segmentation fault, it will arise a compiler error (which is much better).

0
votes

first have a good understanding of pointers, I will give u a short demo:

First let us analyze your code line by line. Lets start from main onwards

char *s = "Some_string";

first of all, you are declaring a pointer to a char variable, now *s is a address in memory, and C will kick you if you try to change its memory value, thats illegal, so u better declare a character array, then assign s to its address, then change s.

Hope you get, it. For further reference and detailed understanding, refer KN King: C programming A Modern Approach

0
votes

Per the language definition, string literals have to be stored in such a way that their lifetime extends over the lifetime of the program, and that they are visible over the entire program.

Exactly what this means in terms of where the string gets stored is up to the implementation; the language definition does not mandate that string literals are stored in read-only memory, and not all implementations do so. It only says that attempting to modify the contents of a string literal results in undefined behavior, meaning the implementation is free to do whatever it wants.