力扣报错runtime error: load of null pointer of type ‘int‘解决思路
记录本算法小白刷力扣的这道题遇到的报错
349. 两个数组的交集https://leetcode.cn/problems/intersection-of-two-arrays/
出现报错的代码
/*** Note: The returned array must be malloced, assume caller calls free().*/
int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){int hash[1000]={0};int result[1000];//交集是去重的,最多只有1000个数for(int i=0;i<nums1Size;i++){//将nums1录入哈希表hash[nums1[i]]++;}int j=0;for(int i=0;i<nums2Size;i++){//将nums2与哈希表比对if(hash[nums2[i]]!=0){result[j]=nums2[i];j++;hash[nums2[i]]=0;//手动去重}}*returnSize=j;//这个是主函数输出时用的,跟内部计算关系不大return result;
}
报错描述
Line 207: Char 3: runtime error: load of null pointer of type 'int' [__Serializer__.c]
原因分析
该题需要return一个数组,而在不申请空间(不malloc)的情况下函数内建立的数组是局部变量,无法带回主函数。
解决方法
在函数里malloc需要返回的数组(其实初始代码顶部的note里说的就是这个)。注意建立数组时在中括号里写数组大小跟malloc的效果在这方面是完全不一样的。
这里用于返回的数组是result,所以在建立result数组时要malloc
修改后的代码
/*** Note: The returned array must be malloced, assume caller calls free().*/
int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){int hash[1000]={0};int *result=(int*)malloc(sizeof(int)*1000);//交集是去重的,最多只有1000个数for(int i=0;i<nums1Size;i++){//将nums1录入哈希表hash[nums1[i]]++;}int j=0;for(int i=0;i<nums2Size;i++){//将nums2与哈希表比对if(hash[nums2[i]]!=0){result[j]=nums2[i];j++;hash[nums2[i]]=0;//手动去重}}*returnSize=j;//这个是主函数输出时用的,跟内部计算关系不大return result;
}
将原来的 int result[1000];
改成了 int *result=(int*)malloc(sizeof(int)*1000);
只修改了这一行,虽然非常浅显但一定要知道的错误
这里因为题目数据限制为
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
数字大小不会超过1000,可以直接开一个大小为1000的数组(虽然烧了不必要的内存)。实际上返回数组是去重的,题目给的两个数组的重复数据个数不会超过数据个数比较小的那个,这样会比较省内存(但我懒得写了)。
力扣报错runtime error: load of null pointer of type ‘int‘解决思路相关推荐
- leetcode报错:member access within null pointer of type struct ListNode
leetcode报错:member access within null pointer of type 'struct ListNode'
- SAP LSMW 事务代码HUPAST的录屏后台执行报错 - Runtime error RAISE_EXCEPTION has occurred - 之分析
SAP LSMW 事务代码HUPAST的录屏后台执行报错 - Runtime error RAISE_EXCEPTION has occurred - 之分析 因项目上成品库存管理启用了handlin ...
- 【项目问题】PM2管理器运行报错: Error: bind EADDRINUSE null:8360(阿里云服务器)
本人后端是使用thinkjs项目创建的,npm run compile 编译后上传到服务器,发现报错:Error: bind EADDRINUSE null:8360 第一步:查看运行日志,报错原因是 ...
- Line 923: Char 9: runtime error: reference binding to null pointer of type ‘int‘ (stl_vector.h)
Leetcode 报错 Line 923: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_ve ...
- 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 ...
- 力扣报错“AddressSanitizer: heap-buffer-overflow on address...”的解决办法
做力扣报了个错: AddressSanitizer: heap-buffer-overflow on address 0x6020000001cc at pc-- 大概意思 LeetCode使用了Ad ...
- LeetCode报错: “runtime error: member access within null pointer of type ‘struct ListNode”
LeetCode里面会经常使用到ListNode这个数据结构,这个报错原因是对空指针进行了引用,但是在测试的时候并不会出现报错,只有在提交的时候才会出现报错. 两者出现区别的原因在于:在测试中给出的测 ...
- LeetCode报错:runtime error: member access within null pointer of type ‘struct ListNode‘
错误题目:876. 链表的中间结点 错误原因:试图使用空指针 解决方法:找出等价判断条件进行替换,排除对空指针的引用. /*** Definition for singly-linked list.* ...
- git报错:error: failed to push some refs to ... 的解决办法及如何让线上覆盖本地方法【Git/SVN】
报错代码 在 push 的时候,出现如下错误: error: failed to push some refs to 'ssh://git@git.umbrella.com:1024/test-pow ...
最新文章
- php 加载一个文件路径_PHP文件加载过程
- 参数估计_数据分析|统计之一个总体参数估计(附代码)
- python with函数的用法(with expression [as target])
- Redis _面试经典
- MySQL学习记录—Date函数系列
- php如何看phpinfo(),如何从phpinfo中获取有效信息 | 学步园
- python 读取文本文件_如何在Python中读取大文本文件
- Cisco 路由器破解密码
- html5给页面添加雨滴特效,JavaScript canvas实现雨滴特效
- 2017年Go语言入门教程-徐培成-专题视频课程
- 安卓开发基础知识3(国内深度摄像头方案)
- jmeter批量上传图片, csv文件参数化——详细讲解
- ubuntu慢?如何给 ubuntu 换源 提速
- 算法优化专题A POJ-2352
- kindle版java核心_深入理解java 7 核心技术与最佳实践-成富[6寸pdf mobi epub kindle版].pdf...
- 计算机专业屏幕尺寸,电脑屏幕尺寸怎么看
- Docker -- 2 -- 利用docker部署网站和数据库
- APP报毒的自查方法
- 【Python】霍兰德人格分析雷达图
- ABBYY FineReader 14都新增了什么功能?