c++ - 运行时错误 : addition of unsigned offset to 0x129000a0 overflowed to 0x12900088

标签 c++

这是我的代码。
已编辑

#include<iostream>
#include<vector>
#include <bits/stdc++.h>

using namespace std;

int main()
{

int n;
cin>>n;
vector<string> v;

 for(int i=0;i<n;i++)
{

    string temp;
    cin>>temp;
     v.push_back(temp);


    //if(v.size()==1){
    //   continue;
    //}

    //cout<<i<<endl;
   // cout<<endl;

//cout<<v.size();

    int k=i;


    while(v[k-1].length()>v[k].length()&&k>-1)
    {
        swap(v[k],v[k-1]);

        k--;
    }
   // cout<<endl;

}




bool check=true;
for(int i=0;i<v.size()-1;i++)
{

 //cout<<v[i]<<endl;
    //cout<<v[i+1]<<endl;

   if (v[i+1].find(v[i]) != std::string::npos) {
//std::cout << "found!" << '\n';
    continue;
    }

   // cout<<"false"<<endl;
   check=false;

}

if(check==true)
{
 cout<<"YES"<<endl;
   for(int i=0;i<n;i++)
   {
       cout<<v[i]<<endl;
   }

}else{


    cout<<"NO"<<endl;
 }



}

这个错误的原因是什么:
输入 曾是:

100
npugmvzdgfnzyxuyfwbzwktiylhvhwgeqauolidpnbemhgbunpefzsltewkxdcrzxgvmkb
bezfumiguzafxghvcfqmwpopxvazctlftelveayycypjckooxeehyk
pig 八戒
elhnhxjwrytbmmqdwwrivvljybhnwfgwhvdgjqgqgvunuemdtrgpyvaanovheqbupamzrjxh
rpvktlmyxfshahfgunrhuqtosysymfjruqlzdooauuihtchzqgyrhcoxbtoorkxkwakvdkiakitlqfbgz
tnrnpghjmqumbzfnztiijgwkiygyfevfebuammkwnoinqvhhlsuoqtfkazqhlnuqtthudhhovjqiuykwqtck
mloehzniuwyakgwmopfgknpoiuiyewijmoefjjjsdimkisugehwqefcx
tthmaxtahimxxts
fspoetalxgcgowhjtanerjpqnen
hefsyokneekdgpbiscss

它适用于其他情况。
Diagnostics detected issues [cpp.clang++-diagnose]: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\vector:1802:11: runtime error: addition of unsigned offset to 0x129000a0 overflowed to 0x12900088
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\vector:1802:11 in

最佳答案

Whats the reason for this error:



您设置 i0
int i=0;

然后你设置 ki . k现在是 0 .
int k=i;

然后您尝试索引 k-1元素 v,即 0-1 , 或 -1 .
while(v[k-1].length()

No bounds checking is performed with std::vector::operator[] 并且您最终将不拥有的内存视为有效构造的 std::string .

关于c++ - 运行时错误 : addition of unsigned offset to 0x129000a0 overflowed to 0x12900088,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50844864/

相关文章:

c++ - GCC: template previously defined 错误

c++ - 实现 C++ grpc 异步客户端时如何轮询 CompletionQueue?

c++ - 在 ';' 之前缺少 'using'

c++ - 获取要使用的 move 构造函数/赋值运算符

c++ - 如何使用 cv::setMouseCallback

c++ - 运行时解释器真的是 C 程序执行的一部分吗?

c++ - 在线程之间发送和读取字符串

c++ - 如何找到大数乘法模 100000007

java - 通用日志解析器算法

C++ COM、Direct2D、Win32 和 WM_CLOSE