0

I am writing up a function that reverses the order of the string "rdd" to "ddr", when I run it I get an error that substring is out of range. Any help is appreciated!

#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h>
using namespace std;

string reverse(const string & s);

int main() {
    cout << reverse("rdd") << endl;
}

string reverse(const string & s) {
    string rname(s);
    for (unsigned i = rname.size()-1; i >= 0; i--) {
        cout << rname[i];
    }
    return rname;
}
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271

3 Answers3

3

This is problem:

for (unsigned i = rname.size()-1; i >= 0; i--) {

Since i is unsigned, i-- will take it from 0 to UINT_MAX. The test i >= 0 can never fail for unsigned int. After this happens you access out of bounds in the loop body.

Instead the loop could look like:

for (unsigned i = rname.size(); i --> 0; )

(using the --> operator), or a better option would be to use C++ idioms:

for (auto it = rname.rbegin(); it != rname.rend(); ++it)   
    cout << *it;

Also see reverse adapters although that might be overkill for this case.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
  • Your loop with --> is brilliant! Never saw this before – PiotrK Dec 18 '15 at 01:01
  • 2
    Are you really encouraging the decidedly confusing `-->` "operator"? Really? Iterator approach is safe and doesn't involve cutesy tricks with syntax. – ShadowRanger Dec 18 '15 at 01:02
  • @ShadowRanger well you could write `i-- > 0` to be normal :) – M.M Dec 18 '15 at 01:03
  • @M.M: Yeah, you could. For C++, I'd stick with the self-documenting use of iterators, but for C, it's not the worst thing in the world (an alternative would be to use an `ssize_t` that technically can't handle an array larger than half the address space, but on the other hand, on most OSes, only half the address space is available in user space anyway, so there is no problem). – ShadowRanger Dec 18 '15 at 01:07
  • `-->` is the so-called `goes to` operator, ha – Chen OT Dec 18 '15 at 06:53
1

Your i is unsigned, therefore the condition i >= 0 is always satisfied. Consider:

unsigned int i = 0;
i--; // i will undeflow, and assume the largest unsigned number possible
if(i < 0) printf("Works?"); // suprise! It will be false and printf does not fire!
PiotrK
  • 4,210
  • 6
  • 45
  • 65
0
for (unsigned i = rname.size()-1; i >= 0; i--) {

The problem is in the above statement because - rname.size() will return length of string. So this loop will run from rname.size()-1to 0(both including) then i-- will be UINT_MAX and condition which is i>=0, will always be true but your string size may less than UINT_MAX so it will return an error which is out of bound error.

Avadhesh
  • 1
  • 1