2

I've been given this code and I'm not quite sure why its behaviour is undefined. My guess is that it has something to do with the memory locations of the two strings and the location(s)' comparison in the if condition.

int main(void) { 
  char *str1 = "xyz"; 
  char *str2 = "xyz";

  if (str1 == str2) {
     printf("Same!\n");
  }  else {
     printf("Not Same!\n");
  }
  return 0; 
}
starcodex
  • 2,198
  • 4
  • 22
  • 34
  • 5
    It is not undefined. Section 6.4.5 defines this behavior. (If it was undefined, the output could be "I like pie!". It's just unspecified.) – David Schwartz Apr 23 '13 at 06:18

4 Answers4

13

It's unspecified (not undefined, there's a subtle distinction) as to whether identical string constants are folded to occupy the same memory.

C++11, 6.4.5 String literals /6 states:

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

Both str1 and str2 are pointers to a block of memory containing the four characters { 'x', 'y', 'z', '\0'} and they are, by definition, non-modifiable.

That means the compiler is free to set both those variables to point to the same block of memory, for efficiency, if it so desires.

Hence str1 and str2 (I'm talking about the pointers, obviously the content behind the pointers is identical) may be identical or not.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I'd further this to say many compilers have merging common literals as a forcible setting that can be forced on, forced off, or left to the discretion of the compiler/linker. – WhozCraig Apr 23 '13 at 06:21
  • It's the same as asking whether the compiler gets the two 3's from different places or whether it gets both 3's from the same place in code like `int i = 3; int j = 3;`. Except because you have a pointer to the initialized value, you can tell which way it does it. The compiler is free to do it either way. – David Schwartz Apr 23 '13 at 06:22
  • I'd be careful there, @David, that's a different situation: (1) `i` and `j` aren't pointers, and (2) the things they represent are modifiable so must be kept distinct. – paxdiablo Apr 23 '13 at 06:24
  • @paxdiablo: That's why I was careful to say "whether it gets both 3's from the same place", not whether it puts them in the same place. And no, they're not modifiable. Those 3's are constants in the *source code*. A program cannot modify its own source code. – David Schwartz Apr 23 '13 at 06:24
  • @WhozCraig, that's a valid point but nothing to do with the standard whatsoever, which is what my answer is focusing on. – paxdiablo Apr 23 '13 at 06:25
  • @paxdiablo It has nothing to do with the standard, but *everything* to do with the specificity (or lack thereof) stated *in* the standard, which was what my comment was focusing on. (and +1, nice answer) – WhozCraig Apr 23 '13 at 06:26
1

"xyz" is a string literal which is put in to "Read-only" section gets mapped into the process space as read-only (which is why you can't change it).

so both str1 and str2 are pointing to same address. This is the reason for printf("Same!\n"); got executed.

This is platform dependent. Refer String literals: Where do they go?

Community
  • 1
  • 1
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
0

C does not support string comparison operator so while comparing strings using == operator it actually compares string addresses and because it is not defined that "xyz" string constant will have same memory address in Read only memory. it is dependent on compiler and machine.

anshul garg
  • 473
  • 3
  • 7
  • What?So you mean two similar strings stored in different locations will be unequal? – Rüppell's Vulture Apr 23 '13 at 06:21
  • 3
    @SheerFish: Two different locations will be unequal. He's comparing addresses. – David Schwartz Apr 23 '13 at 06:24
  • @SheerFish Its up to compiler whether it stores identical string in same memory or different and == operator compares compares base address of string as both ptr will point to starting address of read only string. – anshul garg Apr 23 '13 at 06:24
  • @DavidSchwartz He said `while comparing strings it actually compares string addresses` about C.So `strcmp()` came to my mind where the content of the strings are compared, and the answer is positive if they are same.But yes, in this case the OP is comparing pointers. – Rüppell's Vulture Apr 23 '13 at 06:28
0

Maybe it is undefined because it depends on how compiler optimizes the strings. The result cannot be generally defined without knowing what compiler with what options is used.

Hogan
  • 7,224
  • 2
  • 22
  • 15