【问题标题】:"runtime error: addition of unsigned offset to 0x129000a0 overflowed to 0x12900088"“运行时错误:将无符号偏移量添加到 0x129000a0 溢出到 0x12900088”
【发布时间】:2018-06-13 19:28:03
【问题描述】:

这是我的代码。

#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

它适用于其他情况。

诊断检测到问题 [cpp.clang++-diagnose]:C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\vector:1802:11: runtime错误:将无符号偏移量添加到 0x129000a0 溢出到 0x12900088

摘要:UndefinedBehaviorSanitizer:未定义行为 C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\vector:1802:11 in

【问题讨论】:

  • Unrelated: #include&lt;iostream&gt; #include&lt;vector&gt; #include &lt;bits/stdc++.h&gt; 暗示你不知道 #include &lt;bits/stdc++.h&gt; 做了什么。这是一件好事,因为我们可以把它扼杀在萌芽状态。阅读Why should I not #include <bits/stdc++.h>?

标签: c++


【解决方案1】:

这个错误的原因是什么:

您将i 设置为0

int i=0;

然后将k 设置为0

int k=i;

然后你使用 k-1 来索引std::vector&lt;std::string&gt; v

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

k-1-1,但情况会变得更糟。 std::vector::operator[] 将参数转换为 unsigned 类型,产生一个不可能巨大的、完全无效的索引

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

【讨论】:

    猜你喜欢
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多