Two string literals have the same pointer value?

后端 未结 5 1685
慢半拍i
慢半拍i 2020-11-27 08:14

When I run this program using MinGW, im getting output as \"=\"

#include

using namespace std;

int main()
{
 char *str1 = \"Hello\";
 char *         


        
5条回答
  •  误落风尘
    2020-11-27 09:08

    This is an expected result. You can verify this by looking at the underlying assembly. For example, if I build with:

    g++ -S ptr.c
    

    then you can see the following in the file output (ptr.s):

            .file   "ptr.c"
            .def    ___main;        .scl    2;      .type   32;     .endef
            .section .rdata,"dr"
    LC0:
            .ascii "Hello\0"               ; Note - "Hello" only appears once in
                                           ; this data section!
    LC1:
            .ascii "=\0"
    LC2:
            .ascii "!=\0"
            .text
    .globl _main
            .def    _main;  .scl    2;      .type   32;     .endef
    _main:
            [... some stuff deleted for brevity ...]
    LCFI5:
            call    ___main
            movl    $LC0, -12(%ebp)        ; This sets str1
            movl    $LC0, -8(%ebp)         ; This sets str2
            movl    -12(%ebp), %eax
    

    I've commented the two key bits -- only one appearance of 'Hello' is in the rdata section of the underlying code, and you can see str1 and str2 are set towards the end, both pointing to the same label: LC0. This is beacuse 'Hello' is a string literal and, importantly, is constant.

    As others have pointed out - this is perfectly legal under the standards.

提交回复
热议问题