0

I am doing leetcode 376. Wiggle Subsequence. It went wrong when testing the first instance of input [1,7,4,9,2,5]. It showed that "Line 922: Char 34: runtime error: reference binding to null pointer of type 'value_type' (stl_vector.h)". Could anyone tell me what is going wrong? Thanks a lot!

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        if(nums.size() < 2){
            return nums.size();
        }
        std::priority_queue<int> big_heap;
        vector<int> flag;
        int result;
        int length = nums.size();
        for(int i = 0; i + 1 < length; i++){
            if(nums[i+1] > nums[i]){
            flag[i] = 1;
                }
            else if(nums[i+1] < nums[i]){
                flag[i] = -1;
            }
            else{
                flag[i] = 0;
            }
        }
        int count = 1;
        for(int i = 0; i + 2 < length;i++){
            cout <<flag[i]<<endl;
            if(flag[i] + flag[i+1] == 0){
                count ++;
            }
            else{
                big_heap.push(count);
                count = 1;
            }
        }
        big_heap.push(count);
        result = big_heap.top() + 1;
        return result;

    }
};
2
  • 2
    I suspect that this happens due to vector<int> flag not being initialized. Move the declaration behind the line containing int length = ... and call flag.resize(length); after having declared flag. Commented Aug 30, 2019 at 3:41
  • Welcome to Stack Overflow! Stack Overflow is a question-and-answer site for specific questions about actual code. You need to learn to debug your code. Questions of the form "This code doesn't work" are not on-topic on Stack Overflow and are likely to be downvoted. See How to debug small programs for more information. Commented Aug 30, 2019 at 5:24

1 Answer 1

2

You have undefined behavior, because you are accessing elements of flag while it has size 0.

If you want to write to flag[i] you first need to have an ith element in flag. You can achieve this by resizing flag to the length required, if you know it beforehand. In your case it seems like you will have exactly length-1 elements, so you can do

flag.resize(length-1);

or if you move the declaration after the the one of length, you can directly use std::vectors constructor to do this:

std::vector<int> flag(length-1);

Alternatively you can use push_back to insert elements at the end of the vector, if you are simply writing sequentially new elements, as you seem to be doing here, e.g. instead of flag[i] = 1;:

flag.push_back(1);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help! The problem has been solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.