user3426358 - 1 year ago 164
C++ Question

# Malloc error on vectors in C++

I'm new to C++ and my compiler is throwing me an error which is of no help to understand on what line number the problem perhaps is.

``````#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
vector<int> plusOne(vector<int> &A) {

int start=-1;
for(int i=0;i<A.size()-1;i++)
{
if(A[i]>0)
{
break;
}
if(A[i]==0 && A[i+1]!=0)
{
start=i;
break;
}

vector<int> ans(A.size()-start);
int flag=1;
for(int i=A.size()-1;i>start;i--)
{
if(A[i]==9)
{
if(flag==1)
{
ans[i+1]=0;
flag=1;
}
else
{
ans[i+1]=A[i];
}
}
else
{
ans[i+1]=A[i]+1;
flag=0;
}
}

for(int i=0; i<ans.size(); i++)
{
// cout<<"Here too"<<endl;
cout<< ans.at(i) <<" ";
}
// if(ans[0]==0)
// {
//  vector<int> ans1(ans.size()-1);
//  copy(A.begin()+1,A.end(), ans1.begin());
//  return ans1;
// }
return ans;
}

int main()
{
vector <int> A ={0, 0, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
vector <int> k (plusOne(A));
// cout<<ans.size()<<endl;
for(int i=0; i<k.size(); i++)
{
// cout<<"Here too"<<endl;
cout<< k.at(i) <<" ";
}
cout<<endl;

return 0;
}
``````

When I compile (
`g++ -std=c++11 Add_1.cpp -o Add`
) and run (
`./Add`
), I'm thrown this error:

``````Add: malloc.c:2395: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)
``````

I read up a bit on this, and what I've understood is that such errors are caused perhaps because of wrong use of pointers. I still dont understand what this has to do with my error.

Can someone explain what is wrong with the code and how should it be fixed?

The code is hosted on codepad.

The problem statement of this code is:

Given a non-negative number represented as an array of digits, plus one to the number. Also, the numbers are stored such that the most significant digit is at the head of the list.

Not sure that is the problem you looking for but... if I'm not wrong, it's a problem.

You declare `ans` as

``````vector<int> ans(A.size()-start);
``````

that is a `std::vector` of size `A.size()-start`.

We have that `start` can be `-1` or a value in `[0, A.size()-2]` range.

Consider the case when `start` is greater than zero; by example, suppose `start == 3`. In this case the size of `ans` (`A.size() - 3`) is less than the size of `A`.

The following cycle is

``````for(int i=A.size()-1;i>start;i--)
``````

so the first value for `i` is `A.size() - 1` that is greater of the size of `ans`.

The problem is that you, in this cycle, write in

``````ans[i+1]
``````

so you write in an out of range position in `ans` and without bound check.

This can devastate the memory and (I suppose) can explain your problem.

Suggestion: substitute

``````ans[i+1]=0;
//...
ans[i+1]=A[i];
//...
ans[i+1]=A[i]+1;
``````

with

``````ans.at(i+1) = 0;
//...
ans.at(i+1) = A[i];
//...
ans.at(i+1) = A[i]+1;
``````

This because `at()` perform a bound check and, in case, throw an exception.

If I'm right, you should change your malloc error with an exception.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download