繁体   English   中英

C ++字符串连接有时会失败

[英]C++ string concatenation fails sometimes

我是C ++的血腥初学者。 我尝试读取序列号文件。 为此,我必须创建字符串以包含带有升序文件索引的文件名。

我的代码适用于70以下的索引。当索引为71时,会突然引发异常。

这是我的代码:

for (int i = 0; i < 110; i++)
{
    std::string index = std::to_string(i);
    std::string filenameA = "fileA"+ index + ".png"; // Here the Exception is thrown
    std::string filenameB = "fileB"+ index + ".png";
    std::string filenameC = "fileC"+ index + ".png";

    ...
}

i=71出现读取访问冲突。 此方法在文件xutility中引发异常:

inline void _Container_base12::_Orphan_all() noexcept
    {   // orphan all iterators
 #if _ITERATOR_DEBUG_LEVEL == 2
    if (_Myproxy != nullptr)
        {   // proxy allocated, drain it
        _Lockit _Lock(_LOCK_DEBUG);

        for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
            *_Pnext != nullptr; *_Pnext = (*_Pnext)->_Mynextiter)
            (*_Pnext)->_Myproxy = nullptr;
        _Myproxy->_Myfirstiter = nullptr; // Here the exception is thrown
        }
 #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
    }

奇怪的是,如果使用,代码可以正常工作. ".png"内容丢失。 此外,如果我更改文件的顺序,例如这样

std::string filenameB = "fileB"+ index + ".png";
std::string filenameC = "fileC"+ index + ".png";
std::string filenameA = "fileA"+ index + ".png";

错误仍然发生在std::string filenameA = "fileA"+ index + ".png";

我真的不明白,为什么在这种特殊情况下字符串连接失败。

谢谢你们的评论! 您启发了我再次审视所有内容,所以我发现了我的错误。 简单地尝试在只有64个位置的数组中设置第65个位置。

之前,此代码必须读取一组64个文件,现在我将其扩展为109个文件。 我只是忘了更新数组的大小,这会保存每个文件的信息。

我仍然不明白,为什么异常在第71个索引处而不在第65个索引处抛出,为什么它在字符串连接而不是在数组操作处发生,但是至少该代码现在似乎可以工作了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM