LeetCode报错:Line 1034:Char9
报错信息:Line 1034: Char 9: runtime error: reference binding to null pointer of type ‘std::vector<int, std::allocator>’ (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
class Solution {public:vector<vector<int>> subsets(vector<int>& nums) {vector<vector<int>> ans;vector<int> temp;ans[0]={};//这样的赋值方式是错误的for(int i=0;i<nums.size();i++){int last_ans_size=ans.size();for(int j=0;j<last_ans_size;j++){temp=ans[j];temp.push_back(nums[i]);ans.push_back(temp);}}return ans;}
};
错误分析:ans初始化后可能为空,直接用下标索引的话会导致空指针错误nullptr。
用vs跑这个代码会发生vector越界错误,验证了上述的错误分析。
int main() {vector<vector<int>> ve;ve[0] = {};//错误cout << ve.size() << endl;cout << ve[0].size();return 0;
}
正确的赋值方式应为
int main() {vector<vector<int>> ve;ve.push_back({});//正确的赋值方式ve[0] = { 1 };//给ve赋值后就可以用下标访问ve[0]了cout << ve.size() << endl;cout << ve[0].size();return 0;
}
LeetCode报错:Line 1034:Char9相关推荐
- LeetCode报错:Line 923: Char 9: runtime error: reference binding to null pointer of type ‘std::__cxx11:
LeetCode报错 报错原因: Line 923: Char 9: runtime error: reference binding to null pointer of type 'std::__ ...
- LeetCode 报错解决 heap-buffer-overflow Heap-use-after-free Stack-buffer-overflow Global-buffer-overflow
文章目录 前言 Heap-buffer-overflow Heap-use-after-free Stack-buffer-overflow Global-buffer-overflow 前言 在做L ...
- leetcode报错runtime error: reference binding to null pointer of type ‘std::vector<std::__cxx11::basic_
leetcode报错:runtime error: reference binding to null pointer of type 'std::vector<std::__cxx11::ba ...
- leetcode报错:member access within null pointer of type struct ListNode
leetcode报错:member access within null pointer of type 'struct ListNode'
- hexo g报错,line.mathALL is not funciton问题解决
hexo g报错,line.mathALL is not funciton问题解决 有问必答 ubuntu@VM-16-13-ubuntu:~/hexoblog$ sudo hexo s INFO V ...
- mysql中大数据表alter增加字段报错:1034 Incorrect key file for table 'table_name'; try to repair it...
mysql中大数据表alter增加字段报错:"1034 Incorrect key file for table 'table_name'; try to repair it" 现 ...
- leetcode报错:reference binding to misaligned address 0xbebebebebebec0ba for type ‘int‘, which requir 4
leetcode,剑指offer 31题报错: Line 170: Char 16: runtime error: reference binding to misaligned address 0x ...
- LeetCode报错: “runtime error: member access within null pointer of type ‘struct ListNode”
LeetCode里面会经常使用到ListNode这个数据结构,这个报错原因是对空指针进行了引用,但是在测试的时候并不会出现报错,只有在提交的时候才会出现报错. 两者出现区别的原因在于:在测试中给出的测 ...
- LeetCode报错记录
runtime error: member access within null pointer of type 'ListNode' (solution.cpp) 这个问题主要是由于测试系统不知道当 ...
最新文章
- mysql读取sql脚本_Pandas直接读取sql脚本的方法
- java 快速读文件_java快速读取文件
- mysql任务计划怎么定制_mysql计划任务
- singleton pattern的推荐实现
- 第二十一章 基于鹰栖息(eagle perching)的无模型优化
- 中国行政区划shp地图数据-2022最新数据预览图
- o2o是什么营销模式c语言求幂函数,O2O营销模式的特点是什么,o2o模式的运营方式...
- 之江实验室牵手国科大杭高院 重磅启动全方位合作
- 引谈 | 世界上第一个(真正的)私人家庭安全摄像头诞生!
- 关于Hello Minecraft! Launcher源代码的简介
- 简单好用的在线P图工具,一定记得收藏
- On Die Termination (ODT) DDR
- g代码生成器 源代码_如何生成源代码?
- bzoj5197:[CERC2017]Gambling Guide
- 网页(全屏)背景图实现方式(纯CSS向)
- HDU2717 Catch That Cow ( BFS )
- Scala面向对象详解
- wind7系统修改host
- 基于RTL8211E的千兆以太网收发verilog程序(已经硬件验证,初学=语法注释较多)
- IO流处理2022-11-11