哪个数据段是C字符串存储?

Gal*_*axy 12 c string char

我想知道char s[] = "hello"和之间的区别是什么char *s = "hello".

看完这个这个,我还没有在这个问题上非常清楚.


据我所知,内存中有五个数据段,Text,BSS,Data,Stack和Heap.

根据我的理解,

在以下情况下char s[] = "hello":

  1. "hello" 在文本中.
  2. s 如果是全局变量,则在Data中;如果是局部变量,则在Stack中.

  3. 我们还有一个存储"hello"位置的副本s,因此我们可以修改此字符串的值s.

在以下情况下char *s = "hello":

  1. "hello" 在文本中.
  2. s 如果是全局变量,则在Data中;如果是局部变量,则在Stack中.
  3. s只是指向"hello"Text并且我们没有它的副本,因此通过此指针修改字符串的值应该导致"Segmentation Fault".

我对吗?

shu*_*uva 6

你是对的,第一种情况的“hello”是可变的,而第二种情况的“hello”是不可变的字符串。它们在初始化之前保存在只读存储器中。

在第一种情况下,可变内存是从不可变字符串初始化/复制的。在第二种情况下,指针引用不可变的 string

对于第一种情况维基百科说,

这些变量的值最初存储在只读存储器中(通常在.text 中),并在程序启动例程期间复制到.data 段中。

让我们检查一下segment.c 文件。

char*s = "hello"; // string
char sar[] = "hello"; // string array
char content[32];

int main(int argc, char*argv[]) {
        char psar[] = "parhello"; // local/private string array
        char*ps = "phello"; // private string
        content[0] = 1;
        sar[3] = 1; // OK
        // sar++; // not allowed
        // s[2] = 1; // segmentation fault
        s = sar;
        s[2] = 1; // OK
        psar[3] = 1; // OK
        // ps[2] = 1; // segmentation fault
        ps = psar;
        ps[2] = 1; // OK
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是为segment.c 文件生成的程序集。请注意, 和ssarglobalaka.data段中。似乎sarconst pointer一个可变的 初始化内存或根本不是指针(实际上它是一个数组)。最终它具有sizeof(sar) = 6与 不同的含义sizeof(s) = 8。readonly() 部分中有“hello”和“phello”,.rodata并且实际上是不可变的

    .file   "segment.c"
    .globl  s
    .section    .rodata
.LC0:
    .string "hello"
    .data
    .align 8
    .type   s, @object
    .size   s, 8
s:
    .quad   .LC0
    .globl  sar
    .type   sar, @object
    .size   sar, 6
sar:
    .string "hello"
    .comm   content,32,32
    .section    .rodata
.LC1:
    .string "phello"
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $64, %rsp
    movl    %edi, -52(%rbp)
    movq    %rsi, -64(%rbp)
    movq    %fs:40, %rax
    movq    %rax, -8(%rbp)
    xorl    %eax, %eax
    movl    $1752326512, -32(%rbp)
    movl    $1869376613, -28(%rbp)
    movb    $0, -24(%rbp)
    movq    $.LC1, -40(%rbp)
    movb    $1, content(%rip)
    movb    $1, sar+3(%rip)
    movq    $sar, s(%rip)
    movq    s(%rip), %rax
    addq    $2, %rax
    movb    $1, (%rax)
    movb    $1, -29(%rbp)
    leaq    -32(%rbp), %rax
    movq    %rax, -40(%rbp)
    movq    -40(%rbp), %rax
    addq    $2, %rax
    movb    $1, (%rax)
    movl    $0, %eax
    movq    -8(%rbp), %rdx
    xorq    %fs:40, %rdx
    je  .L2
    call    __stack_chk_fail
.L2:
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
    .section    .note.GNU-stack,"",@progbits
Run Code Online (Sandbox Code Playgroud)

同样,对于main 中的局部变量,编译器不会费心创建name。它可以将其保存在寄存器堆栈存储器中。

请注意,局部变量值“parhello”被优化为 1752326512 和 1869376613 数字。我通过将“parhello”的值更改为“parhellp”发现了它。汇编输出的差异如下:

39c39
<   movl    $1886153829, -28(%rbp)
---
>   movl    $1869376613, -28(%rbp)
Run Code Online (Sandbox Code Playgroud)

所以没有单独的不可变存储psar。它在代码段中被转换为整数。