C++ C++;动态阵列,增加容量
我正在尝试实现一个动态数组,下面是我增加容量的函数C++ C++;动态阵列,增加容量,c++,arrays,dynamic,capacity,C++,Arrays,Dynamic,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++){ ne
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;
}
}
}现在您正在更改
arr
的地址,在该地址中,您必须通过引用传递指针。这样做:
int* changeCapacity(int *&arr, int length, int newCapacity)
我认为你的问题一定来自两个方面:
首先:
changeCapacity(arr, length, capacity);
arr[length] = val;
这里没有新的arr值(由changecacity()返回)。
因此,函数addElement()将返回错误的指针,在下一个addElement()中,这将导致空闲内存损坏
为什么必须获取新的arr值
你和这里一样
a = 1;
changeVar(a);
// value of a here?
int changeVar(int a)
{
a = 5;
return (a);
}
a的值是多少?1,因为changeVar的参数是局部变量
秒:
您在addElement()函数上给出了一个NULL值。这是基于错误消息的猜测,但您已经显示:
int* addElement(int *arr, int& length, int& capacity, int val)
{ //...
changeCapacity(arr, length, capacity);
//...
}
这要求:
int* changeCapacity(int *arr, int length, int newCapacity)
{ //...
delete[] arr;
//...
}
但是,鉴于您目前发布的代码,addElement()
的arr
参数的原始来源未知。你是不是碰巧做了这样的事:
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;
}
foo()
{ int array[N];
//...
addElement(array, ...);
//...
}
或者使用全局数组变量调用addElement()
?在这两种情况下,原始数组都没有通过new[]
分配来匹配delete[]
,这似乎是运行库所抱怨的。错误消息中声明的指针值往往让我认为它最初是在堆栈上分配的
当然,其他问题,如不捕获
changeaccability()
和/或addElement()
的返回值,以及changeaccability()
可能返回空指针的可能性也仍然有效,应该予以修复。这里有一个更好的方法。对于任何想学习的人,评论中都有很好的解释:
#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
}
}
#包括
使用名称空间std;
int*可变容量(int*arr、int-length、int-newCapacity);
整数*加法(整数*arr、整数和长度、整数和容量、整数val);
int main(){
int length=0;//数组中没有初始元素
int capacity=1;//初始容量为1
int*arr=new int[capacity];//为值分配空间
int*temp;//用于存储临时值的指针
/*用于向数组中添加元素的循环*/
对于(int i=0;IIF <代码>长度> NeXultualStudio<代码>,您正在使用内存。您使用的是C++代码:STD::vector < /代码>。这会处理所有这些问题。然后您就可以提前回家。您能告诉我们您如何调用函数吗?为什么您假定(他)是HE?<代码> ARR[长度]?
超出了长度
对象数组的范围,但我认为这不是问题所在。这两个函数都没有调用free()
,因此我认为问题可能出在其他地方……同样,在变更容量()中
,我认为在新请求的容量小于现有容量的情况下返回0可能不是一个明智的设计选择……如果OP使用返回值,这将不需要,因为新指针是从changeCapacity返回的。这就是为什么完整的代码示例很重要。可能真正的问题是h他现在使用的是函数,而不是函数本身。当容量不需要增加时,OP还必须修复返回值0。这是一个错误的选择。或者使用他们自己的返回值。如果在参数中提供引用,为什么返回int*?这段代码中也有内存泄漏。问题中也有同样的错误。我认为最好预先分配在手之前为元素分配空间,而不是每次插入新元素时为元素分配空间。这将要求每次插入时遍历整个数组。我本可以使用链表或向量来解决此问题,但考虑到问题,我认为最好保留问题的原始代码。此外跟踪分配的内存。