gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:06:51 25 4
gpt4 key购买 nike

这是我的代码。
已编辑

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com