1

The first declaration is as follows

char*str="hello";

In this case the string literal is stored in read only memory.

The second declaration is as follows

char str[10]="name";

In this case the string literal is stored in writable memory.

Why there is difference in the allocated memory with the two string literals?

Community
  • 1
  • 1
kevin gomes
  • 1,775
  • 5
  • 22
  • 30
  • 1
    Read [Difference between `char *str` and `char str[]` and how both are stored in memory](http://stackoverflow.com/questions/15177420/what-does-sizeofarray-return/15177499#15177499) – Grijesh Chauhan Jan 30 '14 at 14:25
  • @GrijeshChauhan : I really appreciate your answer in the question given in your comment? – kevin gomes Jan 30 '14 at 14:56
  • @GrijeshChauhan : In your answer you said that at the time when program start the string is copied to the array. Then this means that the string occupies double memory. – kevin gomes Jan 30 '14 at 16:52
  • @kevingomes No it just to explain, The declaration `char str2[] = "hello";` simply means an array with string `"hello"` will be created. – Grijesh Chauhan Jan 30 '14 at 17:08

3 Answers3

5

In the case of char*str="hello"; (which should really by const char*str="hello";) the actual string literal is stored in read-only memory as an array, and str just points to that. In the second example the array is stored either in the global data segment (for global variables) or on the stack (for local data), there's no pointer.

Think of it like this:

For

const char *str = "hello";

you have

+-----+      +-----------+
| str | ---> | "hello\0" |
+-----+      +-----------+

While for

char str[] = "hello";

you have

+-----------+
| "hello\0" |
+-----------+
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
3

There is no difference with the literals themselves. The difference is with the actual variable you're telling the compiler that you want, str. That variable has different types, and thus the different types have different representations.

In the first case, you say "I want str to be a character pointer, initialized to somewhere (I don't care where) where the string "hello" is to be found".

In the second case you say "I want str to be an array of 10 characters, where the five first are initialized with the string "name".

These are clearly completely different things. It's highly likely that in the program for the second case the string literal "name" still exists in some read-only location but is copied into str when the program starts.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • how `name` is copied to `str` without any command. – kevin gomes Jan 30 '14 at 14:25
  • @kevingomes command is given in the form of different syntax, both syntax interpreted differently by compiler. And how does? this is answered. – Grijesh Chauhan Jan 30 '14 at 14:32
  • @kevingomes There is an initializer. It's a feature of the C language that arrays can be initialized by assignment-looking expressions. You can't have `int x[2]; x = { 1, 2 };`, but you *can* have `int x[2] = { 1, 2};`. Same thing, but there is [more sugar](http://en.wikipedia.org/wiki/Syntactic_sugar) for strings. – unwind Jan 30 '14 at 14:36
  • @unwind : In your answer you said `It's highly likely that in the program for the second case the string literal "name" still exists in some read-only location but is copied into str when the program starts.` ...so does it means that the string literal occupies double memory as it is copied to other location. – kevin gomes Jan 30 '14 at 16:53
  • I am confident that `char str[10]="name";` initializes all 10 `char` and not just the first 5, but unable to find a citation - your thoughts? – chux - Reinstate Monica Jan 30 '14 at 17:26
1

String literals have to come from somewhere within the program when the executable is compiled. Thus they are stored along with the text in a special data segment. Since you don't want to modify the program text each time when executing that segment is mapped into your process VM read-only.

If you need to only read that constant literal then a pointer reference is enough.

But as soon as you need to make modifications to it, then you need to make copies like in your second declaration which will copy that string literal onto the stack upon scope start.

Sergey L.
  • 21,822
  • 5
  • 49
  • 75