5

Consider the following code:

char* str = "Hello World";
memcpy(str, "Copy\0", 5);

A segmentation fault occurs during the memcpy. However, using this code:

char str[12];
memcpy(str, "Hello World\0", 12);
memcpy(str, "Copy\0", 5);

The program does not produce a segmentation fault.

Does the problem arise from allocating the memory on the stack versus the data section?

1
  • 4
    Why the double nulls? A string literal already includes the terminating \0, as you can see with sizeof "foobar". Commented Oct 1, 2010 at 22:42

2 Answers 2

14

When you use a string literal in gcc the value is placed in read-only memory and cannot be modified. Trying to modify it leads to undefined behaviour. Usually you will get a segmentation fault on Linux when you try to do this.

The second example works because you aren't modifying the string literal, you are modifying a copy of it that is stored in variable that is not read-only.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 The standard says (6.4.5/6) "If the program attempts to modify such an array, the behavior is undefined." There is no mention of read-only memory though (and it may well be right for Linux and gcc)
The implication of "Hello, World" is that the array is a const char *. Of course you can point to a const char * with a regular char *. If you turn up gcc's warning level, it will complain about this of course. The data is still unmodifiable, however.
@pmg: When the standard says certain behavior is undefined, that means (at least as far as the standard is concerned) a conforming implementation is allowed to do ANYTHING. Reformat the hard drive, trigger thermonuclear Armageddon, etc. If all the programmer is getting is a Segmentation Fault she's getting off easy.
In many cases the "undefined behavior" cases are those that needs hardware support. In this case it is support for memory access rights.
@KFro: String literals are of type char [n] in C, not const char [n]. The situation is different in C++.
2
char* str = "Hello World";

and

char str[12];

are two very different things. One allocates a pointer on the stack and an array in read-only "code segment". The pointer then points at the array. The other allocates the entire array on the stack, and there is no pointer.

6 Comments

Can some one confirm this please. str is it stored in the code segment ? I think its in the data segment.
actually it's called rdata.
Sorry, bad wording on my part. The static constant data and code segment are separate segments. But the memory in both of them cannot be modified.
Actually it's implementation defined whether the string constant is in the code section, in a constant data section or even in a modifiable initialized data section.
@PaulStelian: very true. It still pays to know the difference, for those implementations that do make the difference important.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.