Home Assertion Error on dynamically initializing generic type Array in C++
Reply: 0

Assertion Error on dynamically initializing generic type Array in C++

user1904
1#
user1904 Published in April 25, 2018, 12:37 am
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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302773 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO