Runtime Error

Hi All,
I was trying to solve one problem so written a below solution for that.
Problem :
Given a non-negative number represented as an array of digits,
add 1 to the number ( increment the number represented by the digits ).
The digits are stored such that the most significant digit is at the head of the list.

When I compiled the below code it compiled successfully.
But when I ran it getting bellow error:

Error :: `./solution': double free or corruption (out): 0x0000000000b83010 ***
Aborted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
vector<int> Solution::plusOne(vector<int> &A) 
{    
    vector<int>::iterator it = A.begin();

	// deleting any preceding zeros
	int i = 0;
	int flag = 1;
	while (it != A.end())
	{
		if (*it == 0 && flag == 1)
		{
			i++;
		}
		else
		{
			flag = 0;
			break;
		}
		it++;
	}

	A.erase(A.begin(), A.begin() + i);
	it = A.end();
	do
	{
		--it;
		
		if (it == A.begin() && *it == 9)
		{
			*it = 1;
			A.push_back(0);
			break;
		}
		
		if (*it == 9)
		{
			*it = 0;
		}
		else
		{
			*it = *it + 1;
			break;
		}
	} while (it!=A.begin());	
	return A;
}

Compiler details : C++11 (gcc-4.8)
Looking for help here...
Thanks :)
In your case deque<int> would be a far more suitable container as you're trying to pop numbers from the front:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <deque>

int main()
{
    std::deque<int> num {0, 0, 0, 9, 9, 9, 9};
    while (num.at(0) == 0)
    {
        num.pop_front();
    }
    auto itr = num.rbegin();
   *itr = *itr + 1;
    while(itr != num.rend())
    {
        if(*itr != 10)
        {
            break;
        }
        else
        {
            *itr = 0;
            itr++;
            *(itr) = *(itr) + 1;
         }
    }
    if(num.at(0)==0)
    {
        num.push_front(1);
    }
    for (auto& elem : num)
    {
        std::cout << elem << " ";
    }
}

edit: to be safe check for size 0 at some point as well
Last edited on
Hi Thanks for reply.
And appreciated the alternate solution.

But here I am more interested into knowing the reason of this error in above sample code.

Please can any one help me to identify it?

I think you will have a problem with an array containing only '0's. You will end up deleting all of the elements in your array and then trying to access the value contained in the A.end()--, which is probably just A.end().

Since we don't know what values you passed into your function, it's hard to diagnose run-time errors.

And I think lines 30 and 31 should probably be:

1
2
3
 
		*it = 0;
		A.push_front(1);
closed account (48T7M4Gy)
Looking for help here...

@OP If you are then why don't you submit a small and complete tester that incorporates your post above. What you have so far is like saying to a mechanic 'I've got something wrong with my car, it won't go. Here's the steering wheel.' :)
OP: doug4 gave you a pretty succinct summary of what was going wrong in your attempt and that's the reason I'd mentioned checking for size 0 in my post.
Topic archived. No new replies allowed.