I'm trying to implement a dynamic array and here is my function for increasing the capacity

```
int* changeCapacity(int *arr, int length, int newCapacity) {
    int *newArr = new int[newCapacity];

    if(length > newCapacity){
        return 0;
    } else {
        for(int i = 0; i < length; i++){
            newArr[i] = arr[i];
        }
        delete[] arr;
        arr = newArr;
        return arr;
    }
}

```

This is the error i get:

> 
> speicher(2465,0x7fff7cfc2310) malloc: **\* error for object 0x7f9742403910: pointer being freed was not allocated
>  \*** set a breakpoint in malloc\_error\_break to debug
> 
> 
> 

i'm calling it like this:

```
int* addElement(int *arr, int& length, int& capacity, int val){
if(length >= capacity){
    capacity = capacity * 2;
    changeCapacity(arr, length, capacity);

    arr[length] = val;
    length += 1;
    return arr;
}else{

    arr[length] = val;
    length += 1;
    return arr;
}

```

}