0

Possible Duplicate:
Why do I get a segmentation fault when writing to a string?

The following simple function should reverse a character array in place.

void reverse(char* str)
{
    char* last = str;

    // find end of the string
    while(*last) {
        ++last;
    }

    // swap characters until the pointers meet in the middle
    while(str < last)
    {
        --last; 
        char temp = *str;
        *str = *last;
        *last = temp;
        ++str;
    }
}

int main()
{
    char* a= "Hello";
    reverse(a);
    return 0;
}

The code compiles. But it throws a runtime error about access violation. According to the debugger the culprit is the line below:

char temp = *str;

Any ideas why it happens?

Community
  • 1
  • 1
Alexey
  • 5,898
  • 9
  • 44
  • 81

1 Answers1

6
char* a= "Hello";

The pointer a points to a string literal. According to the standard, attempting to modify a string literal results in undefined behaviour. In the case of your implementation, the segmentation fault indicates that the compiler is choosing to place the string literal in non-modifiable memory.

Declare a to be a string that is modifiable. For example, like this:

char a[] = "Hello";
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 2
    C'mon, you should know better than to answer a question like this with hundreds of duplicates – Ed S. Dec 12 '12 at 21:49
  • thanks! I learnt something. – Alexey Dec 12 '12 at 21:52
  • 2
    @EdS. I don't follow, there's nothing wrong with answering dupes. For a short question like this, an answer is faster than looking for the dupe, and thus helps the op faster. Then you can just vote to close for extra details. – Luchian Grigore Dec 12 '12 at 22:01
  • It's interesting that the answer attracts 3 downvotes. – David Heffernan Dec 12 '12 at 22:04
  • @EdS. that was actually a helpful answer. – Alexey Dec 12 '12 at 22:04
  • 1
    It is helpful, I'm not saying it is not. However, if answering dupes is fine (especially questions like this which are obvious duplicates) then why do we have a category for closing as a duplicate in the first place? On any forum you don't typically want N copies of the same exact question, you just want one good one and then you link back to it. Answering an obvious dupe is just a way to gain rep by answering the same trivial question over and over again. – Ed S. Dec 12 '12 at 22:21
  • 1
    And @Alex, I'm sure it helped you, but a simple search would have helped you as well. Searching for "C char* crash" returns this topic as the first result: http://stackoverflow.com/questions/4226829/c-c-char-pointer-crash – Ed S. Dec 12 '12 at 22:27
  • 1
    I woudl also point out that the standard says nothing of a string literal being stored in "non-modifiable memory", only that attempting to modify such a string results in undefined behavior. Ok, I'll... shut up now :D – Ed S. Dec 12 '12 at 22:35
  • @EdS. Point taken. I got this function from a book on programming. Not the best book I guess. – Alexey Dec 12 '12 at 22:36
  • @Alex The function is fine. It's the way it is called. – David Heffernan Dec 12 '12 at 22:38
  • @Alex: Note that the true type of a string literal (as defined by the standard) is `char[]`, not `char*`. If you want to declare it as a pointer type (which is typical for literals) it should be declared as `const char*`. – Ed S. Dec 12 '12 at 23:03
  • @EdS. Thanks for the edit. I appreciate that. I also re-jigged the wording to remove the ambiguity in the original version. – David Heffernan Dec 12 '12 at 23:07
  • 1
    @DavidHeffernan: No problem, I think it was the right call. You also work on some really interesting stuff at orcina, the type of software I'd enjoy working on myself. Your profile has led me to their site multiple times, so it's the least I could do :). – Ed S. Dec 12 '12 at 23:41