简体   繁体   English

C++ 错误:第 1034 行:字符 9:运行时错误:引用绑定到类型为“int”的 null 指针(stl_vector.h)摘要:UndefinedBehaviorSanitizer:

[英]C++ errror: Line 1034: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h) SUMMARY: UndefinedBehaviorSanitizer:

I am working on LeetCode question 6280 and get the following error:我正在研究 LeetCode 问题 6280 并收到以下错误:

Line 1034: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:9第 1034 行:字符 9:运行时错误:引用绑定到 null 类型为“int”的指针 (stl_vector.h) 摘要:UndefinedBehaviorSanitizer:undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9 /../../../../include/c++/9/bits/stl_vector.h:1043:9

Here is my code:这是我的代码:

class Solution {
public:
    vector<int> closestPrimes(int left, int right) {
        vector<int> prime_list;
        for (int i = left; i <= right; i++) {
            if (isPrime(i))
                prime_list.push_back(i);
        }
        
        vector<int> res;
        res[0] = -1;
        res[1] = -1;
            
        if (prime_list.size() < 2) return res;
        
        int min_dist = prime_list[1] - prime_list[0];
        res[0] = 0;
        res[1] = 1;
        for (int i = 1; i < prime_list.size(); i++) {
            if (prime_list[i] - prime_list[i-1] < min_dist){
                res[0] = i - 1;
                res[1] = i;
            }
        }
        return res;
    }
    
    bool isPrime(int n) {
        if (n <= 1) return false;
        
        for (int i = 2; i < sqrt(n) + 1; i++) {
            if (n % i == 0) return false;
        }
        return true;
    }
    
};

Could anyone help what's the problem?任何人都可以帮助解决问题吗? Thank you!谢谢!

In C++ std::vector don't work like that:在 C++ 中, std::vector不是那样工作的:

        vector<int> res;
        res[0] = -1;
        res[1] = -1;

When you declared variable res it holds empty vector, accessing any elemnt of it is undefined behavior.当您声明变量res时,它包含空向量,访问它的任何元素都是未定义的行为。 You need first add these elements.您需要先添加这些元素。 Either by constructing vector of appropriate size, resizing it or, preferably, by appending values into it:通过构建适当大小的向量,调整它的大小,或者最好是在其中附加值:

        vector<int> res;
        res.push_back(-1);
        res.push_back(-1);

Also if you know size of your data when you writing program and it will be fixed, better approach will be to use std::array .此外,如果您在编写程序时知道数据的大小并且它会被修复,更好的方法是使用std::array In this case your code will work as expected:在这种情况下,您的代码将按预期工作:

        std::array<int, 2> res;
        res[0] = -1;
        res[1] = -1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 第 1034 行:字符 9:运行时错误:引用绑定到“int”类型的 null 指针 (stl_vector.h) - Line 1034: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h) 第 1034 行:字符 9:运行时错误:引用绑定到 leetcode IDE 中类型为“int”(stl_vector.h)的 null 指针 - Line 1034: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h) in leetcode IDE 第 1034 行:字符 9:运行时错误:引用绑定到“int”类型的 null 指针(stl_vector.h)?如何解决&gt;? - Line 1034: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h) ?How to solve it>? 为什么我会收到此运行时错误? 第 1034 行:字符 9:运行时错误:引用绑定到“int”类型的 null 指针 (stl_vector.h) - Why am I getting this runtime error? Line 1034: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h) 错误:第 924 行:字符 9:运行时错误:引用绑定到“int”类型的 null 指针 (stl_vector.h) - Error:Line 924: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h) 第 924 行:字符 9:运行时错误:引用绑定到“int”类型的 null 指针 (stl_vector.h) - Line 924: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h) 运行时错误:引用绑定到“int”类型的空指针(stl_vector.h)c++ - Runtime error: reference binding to null pointer of type 'int' (stl_vector.h) c++ 字符 9:运行时错误:引用绑定到“int”类型的空指针 (stl_vector.h) - Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h) c++ 中的合并排序数组错误:引用绑定到“int”类型的 null 指针 (stl_vector.h) - Merge Sorted array Error in c++: reference binding to null pointer of type 'int' (stl_vector.h) 错误第 923 行:字符 34:运行时错误:引用绑定到类型为“value_type”的空指针 (stl_vector.h) - error Line 923: Char 34: runtime error: reference binding to null pointer of type 'value_type' (stl_vector.h)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM