0

Why does this piece of code throw an exception when I try to change a character in the string

void reverseString(char *s)
{
    int e = strlen(s) - 1;
    int b = 0;
    char t1,t2;

    while(b < e)
    {
        t1 = s[b];
        t2 = s[e];
        s[b] = t2;
        s[e] = t1;
        b++;
        e--;
    }
} 
abelenky
  • 63,815
  • 23
  • 109
  • 159
silencer
  • 2,285
  • 5
  • 18
  • 18
  • 3
    What exception is being thrown? What line is it thrown from? What is an input to the function that generates this behavior? – abelenky Oct 05 '10 at 22:27
  • Have you tried running it under a debugger? – MatthewD Oct 05 '10 at 22:29
  • I figured it out. I was passing in a string, since strings are immutable it was throwing an exception. When I changed it to an array of chars it worked fine. – silencer Oct 05 '10 at 22:29
  • 3
    This is almost FAQ but you need to show the code that calls you function for it to be answered. – CB Bailey Oct 05 '10 at 22:30
  • 1
    How did you manage to pass a `std::string`? The compiler will not let you pass it directly, and if you use the `c_str()` method it will complain about it being `const`. Those kinds of warnings, esp. if you have to cast to make it work, should be a red flag. – Nate Oct 05 '10 at 22:35
  • @user465353: No code in this function throws any exception whatsoever. Either you are referring to `std::string` which is not immutable, or you are referring to a string literal, which is of type `const char[]`. In both cases, passing them to your function would yield a compilation error, not a runtime exception. – André Caron Oct 05 '10 at 22:37
  • possible duplicate of [Why is this C code causing a segmentation fault?](http://stackoverflow.com/questions/1614723/why-is-this-c-code-causing-a-segmentation-fault) – CB Bailey Oct 05 '10 at 22:40
  • @André Caron: sadly, the implicit conversion from string literals (const char *) to char * is permitted for (nowadays ridiculous) compatibility reasons with code written when `const` wasn't a part of the C language. Newer compilers will probably emit a warning, but usually nothing more. IIRC, C99 started to deprecate such conversion. – Matteo Italia Oct 05 '10 at 22:40
  • when he says "string" i think he means "this is my test string" not a std::string – pm100 Oct 05 '10 at 22:55

2 Answers2

4

My guess is that the string you are testing it on is stored in read-only memory. Did you define it with a string literal?

Added later to elaborate:

If you do this,

char *s = "Hello";
reverseString(s);

you will probably crash, because the string can be stored in read-only memory, and most compilers will put it there.

If on the other hand, you write,

char s[] = "Hello";
reverseString(s);

it will work.

Martin York
  • 257,169
  • 86
  • 333
  • 562
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
2

Don't reinvent the wheel, use standard library's algorithms: std::reverse

void reverseString(char *s) {
  std::reverse(s, s + strlen(s));
}

void reverseString(std::string &s) {
  std::reverse(s.begin(), s.end());
}

Note: std::string is mutable.

phadej
  • 11,947
  • 41
  • 78