【问题标题】:Unhandled exception at 0x77e4bef7 in 1.exe: Microsoft C++ exception: std:out_of_range at memory location 0x0012fb8c1.exe 中 0x77e4bef7 处的未处理异常:Microsoft C++ 异常:内存位置 0x0012fb8c 处的 std:out_of_range
【发布时间】:2012-06-15 07:21:43
【问题描述】:

第 25-30 行有问题。我请求了现有索引,但出现错误。没看懂,怎么回事?

    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);
    }

所有代码:http://codepaste.ru/10939/

【问题讨论】:

  • 它的字符串数组,和shells_host一样
  • func.h 是我们不知道的。因此,无法知道LinkToHostLinkToReq(假设它们在func 中定义)是否正确。你为什么使用strings 的数组?使用vector。按值返回strings。让 RVO 启动。是的,你可能有泄漏,因为我的快速扫描找不到delete [] shells_host;
  • 是的,但是使用vector的时候也是同样的问题! codepaste.ru/10940 -- func.h
  • 您是否在调试器中运行过您的应用程序?您是否检查过所有索引是否在您使用的任何数组/向量/集合的范围内?
  • 我们不知道LinkTohost()LinkToReq() 做了什么,但shells_host[i].size()+7 确实看起来有点可疑......无论如何,这应该很容易通过单步调试代码来调试.

标签: c++ memory error-handling outofrangeexception


【解决方案1】:

您将一个空字符串传递给 LinkToHost() 并且对 url.substr(7) 的调用会导致异常。

不用说,在调试器下运行您的代码需要几分钟才能弄清楚这一点。

【讨论】:

    【解决方案2】:

    要么使用调试器深入研究,要么分解问题并添加一些调试打印语句:

    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
    

    【讨论】:

    • 程序没有显示i的值,但是为什么?
    • 谢谢!这是我测试失败!
    猜你喜欢
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    相关资源
    最近更新 更多