C++ dynamic array, increasing capacity -



C++ dynamic array, increasing capacity -

i'm trying implement dynamic array , here function increasing capacity

int* changecapacity(int *arr, int length, int newcapacity) { int *newarr = new int[newcapacity]; if(length > newcapacity){ homecoming 0; } else { for(int = 0; < length; i++){ newarr[i] = arr[i]; } delete[] arr; arr = newarr; homecoming arr; } }

this error get:

speicher(2465,0x7fff7cfc2310) malloc: * error object 0x7f9742403910: pointer beingness freed not allocated * set breakpoint in malloc_error_break debug

i'm calling 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; homecoming arr; }else{ arr[length] = val; length += 1; homecoming arr; }

}

here improve way of doing it. explained in comments wants learn:

#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 1 int* arr = new int[capacity]; // allocating space values int* temp; // pointer storing temporary values /* loop adding elements array */ for(int i=0;i<21;i++){ temp = addelement(arr,length,capacity,i); // adding element array if(temp == null) { // checks if execution successful cout<< "null returned...\n exiting now..."; homecoming 0; // exits programme on failure } arr = temp; // changing value of arr } /* loop printing array */ for(int i=0;i<length;i++){ cout<<arr[i]<<" "; } homecoming 0; } /* function increasing capacity of array */ int* changecapacity(int *arr, int length, int newcapacity) { int *newarr = new int[newcapacity]; // definging new array if(length > newcapacity){ // checking if length of array valid cout<< "invalid length of array\n"; homecoming null; } else { /* loop transferring values new array */ for(int = 0; < length; i++){ newarr[i] = arr[i]; } delete[] arr; // deleting old array (clears memory of old array) // arr = newarr; removed not needed homecoming newarr; // returns new array } } /* function adding new element array */ int* addelement(int *arr, int& length, int& capacity, int val){ if(length >= capacity){ // checks if array has space storing given value or not capacity = capacity * 2; // doubles capacity of array int* temp = changecapacity(arr, length, capacity); // changes size of array new 1 if(temp == null){ // checking if null returned cout<< "failed alter capacity\n"; homecoming null; // returning null } arr = temp; // value of arr not changed in code (problem corrected) arr[length] = val; // stores value in array length += 1; // increasing number of element count of array homecoming arr; // returns new array }else{ arr[length] = val; // stores value in array length += 1; // increasing number of element count of array homecoming arr; // returns new array } }

c++ arrays dynamic capacity

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -