6

I get an error when trying to run the following function:

   char* reverseInPlace(char* src)
{
    //no need to alloc or free memory
    int i=0;
    int size=mystrlen(src);
    for(i=0;i<size;i++)
    {
        int j=size-i-1;

        if(i<j)
        {
            char temp;
            printf("Interchange start %d:%c with %d:%c",i,src[i],j,src[j]);
            temp=src[i];
            src[i]=src[j];//error occurs here
            src[j]=temp;
            printf("Interchange complete %d:%c and %d:%c",i,src[i],j,src[j]);
        }   
    }
    return src; 
}

I call this code like this:

char* rev2=reverseInPlace("BeforeSunrise");
printf("The reversed string is %s\n",rev2);

The error looks like this:

Interchange start 0:B with 12:e
Process terminating with default action of signal 11 (SIGSEGV)
Bad permissions for mapped region at address 0x401165

Why does this error occur?

vamsiampolu
  • 6,328
  • 19
  • 82
  • 183
  • 1
    String literals, aka string constants, are called "constants" for a reason. Pay attention when reading your books/tutorials. –  Jan 27 '14 at 17:53
  • 1
    BTW that question ^^ is the first Google hit. **Literally the first one.** Don't be lazy. –  Jan 27 '14 at 17:54

2 Answers2

9

You are passing a constant string to your function.

String literals are of type char [N + 1] (where N is the length of the array) in C, but modifying them results in undefined behavior. Your compiler should have already issued a warning at that point.

If you wish to modify it then you have to create a copy:

char str[] = "BeforeSunrise";
char* rev2=reverseInPlace(str);
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • 1
    No, string literals are neither const nor pointers. They are non-const arrays, but modifying them results in undefined behavior. Furthermore, the assertion that "[they] are placed during runtime in read-only process memory" may or may not be true, and it is completely irrelevant to the problem. –  Jan 27 '14 at 17:55
  • @H2CO3 isn't it true for even `const` qualified variables in C, that they are not true `const` in the C++ sense and result in undefined behavior when trying to change their value? – ajay Jan 27 '14 at 18:42
  • 1
    @ajay What do you mean by that "they are not true const in the C++ sense"? This has nothing to do with C++, it's a C question. And in C, `const` qualified objects are `const` qualified, and not modifiable. That's all. (Yes, modifying them is UB.) –  Jan 27 '14 at 18:44
2

It's because you try to modify a string literal, which is a constant array, i.e. it's read-only.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    It's read-only, but not constant (assuming you meant "`const`-qualified" by constant), unfortunately. (This is one of the cases where C++ got it right and C got it wrong.) –  Jan 27 '14 at 17:55
  • @H2CO3 so C++ is like 'Don't even try to change a const!' where as C is like 'Well go ahead I don't care' and later 'told ya! treat it as a const!' – ajay Jan 27 '14 at 18:36
  • 1
    @ajay No. It's that string literals are not `const`-qualified in C. Explicitly modifying a `const` object in C is a hard compiler error. –  Jan 27 '14 at 18:45
  • @H2CO3 Thanks for clarifying and dispelling this wrong idea I had. I just checked it and got a flat compile error. Always learning. One step at a time :) – ajay Jan 27 '14 at 18:57
  • 1
    @ajay lesson learned: don't generalize. "String literals behave in a particular way" doesn't mean that "everything in C behaves in that particular way". –  Jan 27 '14 at 18:59