[英]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.