C ++ dynamic array, capacity increase

I am trying to implement a dynamic array, and here is my function to increase 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: the pointer was not freed * set breakpoint in malloc_error_break for debugging

I call 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;
}

}

+1
source share
4 answers

Here is the best way to do this. Everything is well explained in the comments for anyone who wants to study:

        #include <iostream>
        using namespace std;

        int* changeCapacity(int *arr, int length, int newCapacity);
        int* addElement(int *arr, int& length, int& capacity, int val);

        int main(){
            int length = 0; // no inital elements in array
            int capacity = 1; // initial capacity is one
            int* arr = new int[capacity]; // allocating space for values
            int* temp; // pointer for storing temporary values
            /* loop for adding elements to the array */
            for(int i=0;i<21;i++){
                temp = addElement(arr,length,capacity,i); // adding an element to the array
                if(temp == NULL) { // checks if execution was successful
                    cout<< "NULL returned...\n Exiting Now...";
                    return 0; // exits the program on failure
                }
                arr = temp; // changing the value of arr
            }
            /* loop for printing the array */
            for(int i=0;i<length;i++){
                cout<<arr[i]<<" ";          
            }
            return 0;
        }
        /* function for increasing the capacity of array */
        int* changeCapacity(int *arr, int length, int newCapacity) {
            int *newArr = new int[newCapacity]; // definging a new array

            if(length > newCapacity){ // checking if the length of the array is valid
                cout<< "invalid length of array\n";
                return NULL;
            } else {
                /* loop for transferring values to the new array */
                for(int i = 0; i < length; i++){
                    newArr[i] = arr[i];
                }
                delete[] arr; // deleting the old array (clears the memory of the old array)
                // arr = newArr; removed as this is not needed
                return newArr; // returns the new array
            }
        }

        /* function for adding a new element to the array */
        int* addElement(int *arr, int& length, int& capacity, int val){
        if(length >= capacity){ // checks if the array has space for storing the given value or not
            capacity = capacity * 2; // doubles the capacity of the array
            int* temp = changeCapacity(arr, length, capacity); // changes the size of the array to the new one
            if(temp == NULL){ // checking if a null was returned
                cout<< "Failed to change capacity\n";
                return NULL; // returning  NULL
            }
            arr = temp; // the value of arr was not changed in your code (problem corrected)
            arr[length] = val; // stores the value in the array
            length += 1; // increasing the number of element count of the array
            return arr; // returns the new array
        }else{
            arr[length] = val; // stores the value in the array
            length += 1; // increasing the number of element count of the array
            return arr; // returns the new array
        }
        }
-1
source

I assume your problem should come from two things: IMO:

First

changeCapacity(arr, length, capacity);
arr[length] = val;

arr ( changeCapacity()). , addElement() addElement(), .

arr?

,

a = 1;
changeVar(a);
// value of a here?

int changeVar(int a)
{
   a = 5;
   return (a);
}

a? 1, changeVar .

:

NULL addElement().

+1

arr, . :

int* changeCapacity(int *&arr, int length, int newCapacity)
0

, , :

int* addElement(int *arr, int& length, int& capacity, int val)
{ //...
  changeCapacity(arr, length, capacity);
  //...
}

:

int* changeCapacity(int *arr, int length, int newCapacity)
{ //...
  delete[] arr;
  //...
}

arr addElement() , , . - :

foo()
{ int array[N];
  //...
  addElement(array, ...);
  //...
}

, , addElement() ? new[] delete[], , . , , , .

, , changeCapacity() / addElement(), , changeCapacity() NULL, .

0

Source: https://habr.com/ru/post/1589045/


All Articles