待删除

#include 

int main()
{

    char *p = "hello world";
    char str[] = "doanld";

    str[0] = 'D';
    p[0] = 'H'; //运行出错,会提示 bus error

    printf("p[0] = %c, *p = %s, str = %s\n", p[0], p, str);
}

在我电脑上编译得到的汇编代码如下

    .file   "dump.c"
    .intel_syntax noprefix
    .section    .rodata
.LC0:
    .string "hello world"
.LC1:
    .string "p[0] = %c, *p = %s, str = %s\n"
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    push    rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    mov rbp, rsp
    .cfi_def_cfa_register 6
    sub rsp, 32
    mov rax, QWORD PTR fs:40
    mov QWORD PTR [rbp-8], rax
    xor eax, eax
    mov QWORD PTR [rbp-24], OFFSET FLAT:.LC0
    mov DWORD PTR [rbp-16], 1851879268
    mov WORD PTR [rbp-12], 25708
    mov BYTE PTR [rbp-10], 0
    mov BYTE PTR [rbp-16], 68
    mov rax, QWORD PTR [rbp-24]
    mov BYTE PTR [rax], 72
    mov rax, QWORD PTR [rbp-24]
    movzx   eax, BYTE PTR [rax]
    movsx   eax, al
    lea rcx, [rbp-16]
    mov rdx, QWORD PTR [rbp-24]
    mov esi, eax
    mov edi, OFFSET FLAT:.LC1
    mov eax, 0
    call    printf
    mov eax, 0
    mov rsi, QWORD PTR [rbp-8]
    xor rsi, QWORD PTR fs:40
    je  .L3
    call    __stack_chk_fail
.L3:
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609"
    .section    .note.GNU-stack,"",@progbits

你可能感兴趣的:(C)