运行时错误:将无符号偏移量添加到0x129000a0溢出到0x12900088

时间:2018-06-13 19:28:04

标签: c++

这是我的代码。 的 EDITED

#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 ingenqhogs elhnhxjwrytbmmqdwwrivvljybhnwfgwhvdgjqgqgvunuemdtrgpyvaanovheqbupamzrjxh rpvktlmyxfshahfgunrhuqtosysymfjruqlzdooauuihtchzqgyrhcoxbtoorkxkwakvdkiakitlqfbgz tnrnpghjmqumbzfnztiijgwkiygyfevfebuammkwnoinqvhhlsuoqtfkazqhlnuqtthudhhovjqiuykwqtck mloehzniuwyakgwmopfgknpoiuiyewijmoefjjjsdimkisugehwqefcx tthmaxtahimxxts fspoetalxgcgowhjtanerjpqnen hefsyokneekdgpbicss

适用于其他情况。

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

1 个答案:

答案 0 :(得分:2)

  

出现此错误的原因是什么:

您将i设为0

int i=0;

然后你将k设置为i

int k=i;

然后索引v。

-1元素
while(v[k-1].length()

No bounds checking is performed with std::vector:>:operator[]你最终会把你不拥有的记忆当作一个有效构建的std::string处理。