Line 1034: Char 9: runtime error: reference binding to null pointer of type ‘int’ (stl_vector.h)

  array-merge, c++

I was solving the leetcode question Merge Sorted Array and I am getting runtime error while running the following code on leetcode

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has a size equal to m + n such that it has enough space to hold additional elements from nums2.

Example 1:

Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]

my code :

class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
    int i = 0, j = 0, k = m-1;
    
    while(i<=k && j<m) {
        if(nums1[i] < nums2[j]) {
            i++;
        }
        else {
            swap(nums2[j++], nums1[k--]);
        }
    }
    sort(begin(nums1), begin(nums1) + m );
    sort(begin(nums2), end(nums2));
    
    k = m + 1;
    for(int l = 0; l < n; l++) {
        nums1[k] = nums2[l];
        k++;
    }
}
};

Source: Windows Questions C++

LEAVE A COMMENT