我正在尝试实现动态数组,这是我增加容量的功能
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;
}
}
答案 0 :(得分:1)
我认为你的问题必须来自两件事IMO :
<强>第一强>:
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的参数是局部变量。
<强>第二强>
您在 addElement()函数中提供 NULL 值。
答案 1 :(得分:0)
现在您正在更改arr
的地址,您必须通过引用传递指针。这样做:
int* changeCapacity(int *&arr, int length, int newCapacity)
答案 2 :(得分: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指针的可能性仍然有效,并且应该修复。
答案 3 :(得分:-1)
这是一种更好的方法。对于任何想要学习的人,评论中都能很好地解释一切:
#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
}
}