mardi 5 décembre 2017

shared pointer memory error

I'm on new shared_ptr. I want to use it but I cant solve this problem.İf I dont write last insert its okey but I want to continue insert and and allocate memory.Even if I do not write anything into the IF block, it gives a error.

#include <iostream>
#include <memory>

using namespace std;

template <typename T>
class A{
public:
    int insert(T in);
    int end() {
        cout << A<T>::sp.get()[6] << endl;
        cout << A<T>::_size << "---> size" <<endl;
        cout << A<T>::_capacity << "----> c"<<endl;
        return 0;
    }

protected:
    shared_ptr<T> sp{ new T[10], default_delete<T[]>() };
    int _size=0;
    int _capacity=10;
};

template <class T>
int A<T>::insert(T in){
    if (A<T>::_capacity >= A<T>::_size) {
        A<T>::sp.get()[A<T>::_size] = in;
        A<T>::_size++;
    }
    else if (A<T>::_capacity < A<T>::_size) {
        //...
    }
 }

int main() {
    A<int> a;
    a.insert(0);
    a.insert(1);
    a.insert(2);
    a.insert(3);
    a.insert(4);
    a.insert(5);
    a.insert(6);
    a.insert(7);
    a.insert(8);
    a.insert(9);
    a.insert(9);

    //cout<<a.empty();
    a.end();
    return 0;
 }

This is the error:

Error in `./main': free(): invalid next size (fast):

0x0000000001f6ec20 ***

Aucun commentaire:

Enregistrer un commentaire