2

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.

Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57
  • possible duplicate of [C: differences between pointer and array](http://stackoverflow.com/questions/1335786/c-differences-between-pointer-and-array) – Oliver Charlesworth Jan 24 '13 at 10:42

5 Answers5

3

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.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • @OliCharlesworth Doh. I should have known that was too simply put. Rewritten, and thanks. – unwind Jan 24 '13 at 11:07
  • @unwind there would be no *question* if string literals were `const char*`: the code just wouldn't compile (that's why I downvoted -- and I'm removing the downvote as you corrected it). That said, in all the answers so far there's no attempt to explain the *purpose* of moving string literals to read-only sections. (It's unclear whether the question asks for the purpose or "that's just how it works" is enough). – Anton Kovalenko Jan 24 '13 at 11:39
3

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.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • 1
    In the first case I also expect the compiler issuing a warning about assigning a const to non-const or something. – junix Jan 24 '13 at 10:46
2
  • 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).

Maroun
  • 94,125
  • 30
  • 188
  • 241
0

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

user1971996
  • 53
  • 2
  • 9
0

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.

John Bode
  • 119,563
  • 19
  • 122
  • 198