-1
template <class E>
bool ArrayList<E>::add(E* obj){
    if(this->insure_capacity()){
        this->_size++;
        // cout<<"add ArrayList "<<_size<<endl;
        this->_array[this->_size] = *obj;
        return true;
    }
    return false;
}



template <class E>
bool ArrayList<E>::insure_capacity(){
    if(_size < _capacity){
        return true;
    }else{
        return grow();
    }
};


template <class E>
bool ArrayList<E>::grow(){
    cout<<"grow"<<endl;
    int old_capacity = this->_capacity;
    if(this->_capacity == 1){
        this->_capacity == _DEFAULT_CAPACITY;
    }else{
        this->_capacity += old_capacity/2;
    }

    E* temp_array = new E[this->_capacity];//this line gives error
    for(int i = 0 ; i < this->_capacity ; i++){
        temp_array[i] = i < old_capacity ? _array[i]:0;
    }
    return true;
};  

In Above Function When I initialize temp_array it gives following error

malloc.c:2373: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size)

= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.

But when I paste same line in Constructor of the class. it executes successfully.

I don't know why this error occurs. I have searched for it but don't get any satisfactory answer. Can some one explain it in detail why this error occurs and how to resolve it. Thanks.

Navin Gupta
  • 793
  • 6
  • 20

1 Answers1

0

OK! So finally I got the answer of this question.

The actual problem was in my add function where I am adding an object to the Array-List. Here before adding an object in List I was incrementing the _size variable by 1. So the first object was added to index 1 and last object was added at _array[_capacity](say if initial capacity is 10 , initial array size , then my last element was added at 10th position Which is out of bound of the array.)

These are the following steps which produces error.

1- I initialized _array in constructor with a size of 10 which is _DEFAULT_CAPACITY of array through new operator. let's assume the starting address of the array is 1000 and each element has an address of 4 byte; so the array will occupy 40 bytes of memory and it will ended at location 1040 .

2- Then I added 10 elements to that array but as I discussed above my last element was added at _array[10] (in C++ it doesn't throw ArrayOutOfBoundException); Due to this reason the starting location of _array[10] element is 1040 now.

3- When I was added 11th element to array I have to increase the size of _array so I created a temp_array of bigger size and it throws error. But actual problem starts from here.

temp_array is initialized just after the address of _array's address. Because C++ complier initialized next variable to the previous address returned by malloc function.(Here previous location will be (1040+some ofsset) as _array ends here) but this address is already occupied by _array's 11th element i.e _array[10] which is at 1040. So there is a conflict and it throws Assertion Error.

I analyze this issue through valgrind tool. This tool can be found at http://valgrind.org/ this location.

Detailed answer is provided by Jon Gjengset.Thanks to him.

https://stackoverflow.com/a/19365957/5519615

Another good answer bout usage of valgrind and about memory issues is given by Peri461

https://stackoverflow.com/a/44989219/5519615

Navin Gupta
  • 793
  • 6
  • 20