报错信息: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相关推荐

  1. 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::__ ...

  2. 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 ...

  3. 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 ...

  4. leetcode报错:member access within null pointer of type struct ListNode

    leetcode报错:member access within null pointer of type 'struct ListNode'

  5. 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 ...

  6. 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" 现 ...

  7. 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 ...

  8. LeetCode报错: “runtime error: member access within null pointer of type ‘struct ListNode”

    LeetCode里面会经常使用到ListNode这个数据结构,这个报错原因是对空指针进行了引用,但是在测试的时候并不会出现报错,只有在提交的时候才会出现报错. 两者出现区别的原因在于:在测试中给出的测 ...

  9. LeetCode报错记录

    runtime error: member access within null pointer of type 'ListNode' (solution.cpp) 这个问题主要是由于测试系统不知道当 ...

最新文章

  1. mysql读取sql脚本_Pandas直接读取sql脚本的方法
  2. java 快速读文件_java快速读取文件
  3. mysql任务计划怎么定制_mysql计划任务
  4. singleton pattern的推荐实现
  5. 第二十一章 基于鹰栖息(eagle perching)的无模型优化
  6. 中国行政区划shp地图数据-2022最新数据预览图
  7. o2o是什么营销模式c语言求幂函数,O2O营销模式的特点是什么,o2o模式的运营方式...
  8. 之江实验室牵手国科大杭高院 重磅启动全方位合作
  9. 引谈 | 世界上第一个(真正的)私人家庭安全摄像头诞生!
  10. 关于Hello Minecraft! Launcher源代码的简介
  11. 简单好用的在线P图工具,一定记得收藏
  12. On Die Termination (ODT) DDR
  13. g代码生成器 源代码_如何生成源代码?
  14. bzoj5197:[CERC2017]Gambling Guide
  15. 网页(全屏)背景图实现方式(纯CSS向)
  16. HDU2717 Catch That Cow ( BFS )
  17. Scala面向对象详解
  18. wind7系统修改host
  19. 基于RTL8211E的千兆以太网收发verilog程序(已经硬件验证,初学=语法注释较多)
  20. IO流处理2022-11-11

热门文章

  1. flash iphone效果
  2. 单项选择标准化考试系统
  3. (侯捷C++)1.1面向对象高级编程(上)
  4. keep健身教程合集(阿里云盘)
  5. 视觉SLAM十四讲学习笔记-第三讲-旋转向量、欧拉角、四元数
  6. Java培训机构出来好找工作吗?
  7. SSD-6D: Making RGB-Based 3D Detection and 6D Pose Estimation Great Again—2017(笔记)
  8. OO第二单元电梯作业总结
  9. arm架构 CF-WU810N网卡驱动安装经验
  10. 梅卡尔大学-IOT-前端笔记