The addresses of two pointers to different string literals are the same

#include<stdio.h> #include<string.h> int main() { char * p = "abc"; char * p1 = "abc"; printf("%d %d", p, p1); } 

When I print the values ​​of two pointers, it prints the same address. What for?

+80
c pointers literals
Sep 30 '13 at 6:56
source share
10 answers

Whether two different string literals with the same contents are in the same memory location or in different memory cells depends on the implementation.

You should always consider p and p1 as two different pointers (although they have the same content), as they may or may not indicate the same address. You should not rely on optimizing the compiler.

C11 Standard, 6.4.5, String Literals, Semantics

It is not indicated whether these arrays are different if they have corresponding values. If the program tries to change such an array, the behavior is undefined.




The format for printing should be %p :

  printf("%p %p", (void*)p, (void*)p1); 

See this answer for what.

+85
Sep 30 '13 at 7:06 on
source share

Your compiler seems pretty smart, finding that both literals are the same. And since literals are constant, the compiler decided not to store them twice.

It seems worth mentioning that this does not have to be so. Please see Blue Moon answer this .




Btw: the printf() statement should look like this:

 printf("%p %p", (void *) p, (void *) p1); 

how "%p" used to print pointer values ​​and is only defined for a pointer of type void * . * one




In addition, I would say that the code skips the return , but appears to be changing in the C file. Others may make this clear.




* 1: For char * there is no need to specify void * , but for pointers to all other types.

+28
30 sept. '13 at 6:58
source share

Your compiler has done something called string concatenation. You indicated that you need two pointers, both pointing to the same string literal, so it made only one copy of the literal.

Technically: he should have complained about you for not specifying "const" pointers

 const char* p = "abc"; 

Perhaps this is due to the fact that you are using Visual Studio or using GCC without -Wall.

If you want them to be stored twice in memory, try:

 char s1[] = "abc"; char s2[] = "abc"; 

Here you explicitly state that you need two arrays of c-string characters, not two pointers to characters.

Caveat: String pooling is a compiler / optimizer function, not a language facet. Since such different compilers in different environments will create different behavior depending on factors such as optimization level, compiler flags and string values ​​in different compilation units.

+18
Sep 30 '13 at 7:01
source share

As others have said, the compiler notices that they have the same value, and therefore decides that they share the data in the final executable. But it becomes more attractive: when I compile the following with gcc -O

 #include<stdio.h> #include<string.h> int main() { char * p = "abcdef"; char * p1 = "def"; printf("%d %d", p, p1); } 

he prints 4195780 4195783 for me. That is, p1 starts 3 bytes after p , so GCC will see the general suffix def (including the terminator \0 ) and performed a similar optimization with the one you showed.

(This is the answer because it is too long to be a comment.)

+14
Sep 30 '13 at 12:28
source share

String literals in code are stored in a read-only code data segment. When you write a string literal such as "abc", it actually returns "const char *", and if you had all the compiler warnings, it will tell you what you are throwing at that moment. You are not allowed to change these lines for the reason that you indicated in this question.

+3
Sep 30 '13 at 7:00
source share

When you create a string literal ("abc"), it is stored in memory that contains string literals, and then it is reused if you reference the same string literal, so both pointers point to the same location. where is the string literal "abc" stored.

I found out about this a while ago, so I might not have explained it really clearly, sorry.

+2
Sep 30 '13 at 7:00
source share

Actually depends on which compiler you are using .

On my system with TC ++ 3.5, it prints two different values ​​for two pointers, i.e. two different addresses .

Your compiler is designed, it will check for the presence of any value in memory, and depending on its existence, it will reassign or , using the same link, a previously saved value, if the same value is specified.

So don’t think about it too much, because it depends on how the compiler parses the code.

IT'S ALL...

+2
Sep 30 '13 at 8:33
source share

because the string "abc" itself is an address in memory. when u write "abc" again, it keeps the same address

+1
Sep 30 '13 at 6:59
source share

This is compiler optimization, but forget to optimize portability. Once compiled codes are more readable than actual codes.

+1
Oct 01 '13 at
source share

a string literal is used,

when complier catches two identical string literals,

it gives the same memory location, so it shows the same pointer location. /

0
Sep 30 '13 at 13:34 on
source share



All Articles