-2

There's something wrong in rows 25-30. I requested the existing index but get an error. I don't understand the problem, what's wrong?

    string *shells_host = new string[cnt];
    for(int i=0;i<cnt;i++)
    {
        shells_host[i] = LinkToHost(shells[i]);
        shells[i] = LinkToReq(shells[i],shells_host[i].size()+7);
    }

All code: http://codepaste.ru/10939/

Chris Barlow
  • 3,274
  • 4
  • 31
  • 52
bagi
  • 21
  • 2
  • 8
  • Its array of string, same as shells_host – bagi Jun 15 '12 at 07:27
  • 2
    `func.h` is something that we don't know. So, no way to know if `LinkToHost` and `LinkToReq` (assuming they are defined in `func`) are correct. Why are you using an array of `string`s? Use a `vector`. return `string`s by value. Let RVO kick-in. And yes, you probably have a leak since my quick scan couldn't find a `delete [] shells_host;`. – dirkgently Jun 15 '12 at 07:28
  • Yes, but when using the vector is the same problem! http://codepaste.ru/10940/ -- func.h – bagi Jun 15 '12 at 07:32
  • 1
    Have you run your application in a debugger? Have you checked that all indexes are withing range of whatever array/vector/collection you are using? – Some programmer dude Jun 15 '12 at 07:33
  • We don't know what `LinkTohost()` or `LinkToReq()` do, but `shells_host[i].size()+7` certainly looks a bit fishy... In any case, this should be easy to debug by stepping through the code. – Michael Burr Jun 15 '12 at 07:34
  • Yes, I ran the debugger and saw a few strings of well handled, but then for some reason an error occurs – bagi Jun 15 '12 at 07:35
  • About `LinkToReq(shells[i],shells_host[i].size()+7);`, I can't say anything about `LinkToReq` since you don't show it anywhere, but what if some length of something inside is less (or more) than `shells_host[i].size()+7`? – Some programmer dude Jun 15 '12 at 07:35
  • These functions are cut off the link. First, in the shell fall line, then they are transformed into the domain and request – bagi Jun 15 '12 at 07:39
  • You might want to stick some debug output in there so you can see what the actual values stored in `shells` are. Any chance that one of them is under 7 characters long, for example? – Rook Jun 15 '12 at 08:23
  • No, this is a reference to a resource – bagi Jun 15 '12 at 08:25
  • That doesn't really answer my question. A short string would cause an error, so you should be checking for short strings. – Rook Jun 15 '12 at 08:47

2 Answers2

3

You pass an empty string to the LinkToHost() and the call to url.substr(7) causes the exception.

Needless to say, that it takes a few minutes to run your code under debugger to figure out this.

Dmitry Egorenkov
  • 1,045
  • 8
  • 19
0

Either dive in with a debugger or break up the problem and add some debug print statements:

for(int i=0;i<cnt;i++)
{
    printf("i=%d, shells=%d", i, shells[i]);
    shells_host[i] = LinkToHost(shells[i]);

    int secondArg = shells_host[i].size()+7;
    printf("shells_host=%d, secondArg", shells_host[i], secondArg);
    shells[i] = LinkToReq(shells[i], secondArg);
}//to here
Jon Cage
  • 36,366
  • 38
  • 137
  • 215