동적 배열을 구현하려고하는데 여기에 용량을 늘리는 기능이 있습니다.
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;
}
}
이것은 내가 얻는 오류입니다.
speicher (2465,0x7fff7cfc2310) malloc : * 0x7f9742403910 개체에 대한 오류 : 해제되는 포인터가 할당되지 않았습니다. * 디버그를 위해 malloc_error_break에 중단 점을 설정합니다.
나는 그것을 다음과 같이 부른다.
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;
}
}
여기에 더 나은 방법이 있습니다. 배우고 자하는 모든 사람들을위한 코멘트에 모든 것이 잘 설명되어 있습니다.
#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
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다