1

我正在尝试实现一个动态数组,这是我增加容量的函数

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

}

4

4 回答 4

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值。

于 2014-06-19T15:47:41.483 回答
0

现在您正在更改 的地址arr,您必须在其中通过引用传递指针。做这个:

int* changeCapacity(int *&arr, int length, int newCapacity)
于 2014-06-19T15:27:52.153 回答
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()用全局数组变量调用?在任何一种情况下,原始数组都不是通过 a 分配new[]来匹配 的delete[],这似乎是运行时库所抱怨的。错误消息中指出的指针值往往让我认为它最初是在堆栈上分配的。

当然,其他问题,比如不捕获changeCapacity()and/or的返回值,以及可能返回 NULL 指针addElement()的可能性changeCapacity()仍然有效,应该修复。

于 2014-06-19T19:25:54.707 回答
-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
        }
        }
于 2014-06-22T23:00:04.697 回答