1

I assign two different pointers with the same content(not value!) to strings and when I print they address I saw they have the same adders and the none pointer string has different adders, can someone explain why is it happening? Someone told me it had something to do with string literals, but I don't sure.

char* str1 = "hello";
char* str2 = "hello";
char str3[8] = "hello";

printf("%p %p %p", str1, str2, str3);

output:

0x01077B30
0x01077B30
0x0107F6E8

2 Answers2

3

When you have a string literal, the actual string is stored in a location that is possibly read-only and for that reason shouldn't be modified. The char* you get points to that place. So when you have the exact same literal multiple times, it doesn't need multiple copies of the same string and it can just make them point to the same actual string. However, this is not guaranteed to happen, it's up to the compiler.

When you do this on the other hand:

char str3[8] = "hello";

You get a copy of the string on the stack, so that's why its address is different.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • Does the compiler build some sort of binary search tree in case a program hosts many different sting literals to compare and decide to allocate or not? – machine_1 Apr 17 '19 at 10:53
  • @machine_1 I guess there's a lot of potential for optimization here, but the standard doesn't require anything like that to be done. There's not even a guarantee that two equal string literals will only result in one copy of it in the program. On my machine, when I have two `"hello"` they will return the same address, but if I have `"ello"` it is stored separately, even though it is in the `"Hello"` string. It's entirely up to the implementation. There's a good question that goes into detail on this here: https://stackoverflow.com/q/52423837/10411602 – Blaze Apr 17 '19 at 11:34
  • @EricPostpischil good point. I edited the answer to reflect how it's not necessarily read-only, just shouldn't be modified. – Blaze Apr 17 '19 at 12:14
0

It's your compiler optimization in effect.

Your compiler decided to allocate memory for only one string literal, "hello", and make both str1 and str2 to point to that memory. Given the nature of string literals (i.e., no modifications are expected), this usage would be sufficient.

However, if you try to print the address of str1 and str2, you would see they are different.

On the other hand, str3, is, however, an array, initialized with the content from the string literal "hello", that's why it is stored in a separate location in memory.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261