1

Possible Duplicate:
Why does this Seg Fault?

Is the stack allocation is read only:

char* arr="abc";
arr[0]='c';

Can you change the string that is allocated on the stack??

Community
  • 1
  • 1
MoonBun
  • 4,322
  • 3
  • 37
  • 69
  • http://stackoverflow.com/questions/2036096/literal-string-initializer-for-a-character-array – kan Oct 05 '11 at 09:53

3 Answers3

7

The string "abc" isn't on the stack. A pointer to it (arr) is. Modifying the string literal is undefined behaviour.

You can see this quite clearly in the asm GCC generates on x86:

        .file   "test.c"
        .section        .rodata
.LC0:
        .string "abc"             ; String literal inside .rodata section
        .text
.globl main
        .type   main, @function
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $16, %esp
        movl    $.LC0, -4(%ebp)   ; Pointer to LC0 (our string onto stack)
        movl    -4(%ebp), %eax    ; Pointer is copied into eax register
        movb    $99, (%eax)       ; Copy $99 ('c') to what eax points to (in .rodata)
Flexo
  • 87,323
  • 22
  • 191
  • 272
5

Your code doesn't allocate a string on the stack. It allocates a char* on the stack, that is to say a pointer, and it makes that pointer point at a string literal. Attempting to modify the string literal is undefined behavior.

To allocate the string on the stack, do:

char arr[] = "abc";

Now you've taken a copy of the string literal in your stack-allocated array arr, and you're allowed to modify that copy.

For full pedantry: everything I've described as "stack-allocated" are technically "automatic variables". C itself doesn't care where they're allocated, but I can guess with a lot of confidence that your implementation in fact does put them on a stack.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • +! for the pedantry I almost started to write in my answer – Flexo Oct 05 '11 at 09:57
  • However, there is nothing in the original post indicating that this will become an automatic variable, it depends on in what scope the variable was allocated in. If it is declared at file scope, it will end up together with other variables with "static storage duration", somewhere in RAM, at some implementation-defined location. – Lundin Oct 05 '11 at 11:09
  • 1
    @Lundin: if the questioner's code isn't in function scope, then `arr[0] = 'c';` definitely isn't allowed. – Steve Jessop Oct 05 '11 at 11:19
  • @Steve Well... there is nothing saying that the two source code lines are located after each other :) But point taken. Btw since we are in full pedantry land, I take it you mean block scope. Function scope, as defined by the standard, is only for goto-labels :) – Lundin Oct 05 '11 at 11:31
  • @Lundin: OK, I'll take your word for it without checking :-) – Steve Jessop Oct 05 '11 at 11:32
3

"abc" is not allocated on the stack, it is a string literal.

No, you can't modify it. Your compiler can put that string in a read-only memory segment (if your implementation has such a concept). Trying to change it leads to undefined behavior.

(It crashes on Linux with GCC with default compile options for instance.)

Mat
  • 202,337
  • 40
  • 393
  • 406