我正在尝试解决我正在实现动态数组的Web教程(非标记)的问题。但是,它在两个地方失败了:
ResizeDynamicArray(...)
输出的数据不正确。我希望看到:
10 20 30 40 50
10 30 40 50
10 30 50
...但我得到了这个:
8723216 8716480 701955994 201375930 10
8723216 701955994 201375930 10
8723216 701955994 10
解决方案的要求是:
malloc
/ free
vector
s 有人可以建议我做错了吗?
int* CreateDynamicArray(int capacity, int &size);
void DeleteDynamicArray(int* dynamicArray, int &size);
void InsertElement(int* dynamicArray, int element, int &size, int &capacity);
void DeleteElement(int* dynamicArray, int elementIndex, int &size);
void ResizeDynamicArray(int** dynamicArray, int newCapacity);
void PrintArray(int *ptrArray, int capacity);
#include <iostream>
int main()
{
int size = 0;
int capacity = 0;
int* ptrArray = CreateDynamicArray(capacity, size);
InsertElement(ptrArray, 10, size, capacity);
InsertElement(ptrArray, 20, size, capacity);
InsertElement(ptrArray, 30, size, capacity);
InsertElement(ptrArray, 40, size, capacity);
InsertElement(ptrArray, 50, size, capacity);
PrintArray(ptrArray, size);
DeleteElement(ptrArray, 1, size);
PrintArray(ptrArray, size);
DeleteElement(ptrArray, 2, size);
PrintArray(ptrArray, size);
DeleteDynamicArray(ptrArray, size);
ptrArray = nullptr;
return 0;
}
int* CreateDynamicArray(int capacity, int &size)
{
size = 0;
return new int[capacity];
}
void DeleteDynamicArray(int* dynamicArray, int &size)
{
if (dynamicArray != nullptr) {
delete[] dynamicArray;
dynamicArray = nullptr;
}
size = 0;
}
void InsertElement(int* dynamicArray, int element, int &size, int &capacity)
{
if (size > capacity)
capacity += 2;
else
capacity += 1;
ResizeDynamicArray(&dynamicArray, capacity);
dynamicArray[size] = element;
size++;
}
void DeleteElement(int* dynamicArray, int elementIndex, int &size)
{
for(int i = elementIndex + 1; i < size; i++)
dynamicArray[i - 1] = dynamicArray[i];
size--;
}
void ResizeDynamicArray(int** dynamicArray, int newCapacity)
{
int *newArray = new int[newCapacity];
for(int i = 0; i < newCapacity; i++) {
newArray[i] = (*dynamicArray)[i];
}
// if (*dynamicArray != nullptr) {
// delete [] *dynamicArray;
// }
*dynamicArray = newArray;
}
void PrintArray(int *dynamicArray, int size)
{
for (int i = 0; i < size; i++) {
std::cout << dynamicArray[i] << " ";
}
std::cout << std::endl;
}
答案 0 :(得分:0)
让我们看一下InsertElement
中的问题。你有:
void InsertElement(int* dynamicArray, int element, int &size, int &capacity)
{
...
ResizeDynamicArray(&dynamicArray, capacity);
...
}
在此函数中,您可以修改函数中本地dynamicArray
点的内容。这不会改变指针在调用函数中指向的内容。因此,您有以下问题。
InsertElement
返回后,函数中获取的内存将丢失到程序中。修复它的方法是通过引用传递指针。
void InsertElement(int*& dynamicArray, int element, int &size, int &capacity)
{
...
ResizeDynamicArray(&dynamicArray, capacity);
...
}
我建议更改ResizeDynamicArray
以通过引用接受指针。使用int**
是C风格,而不是C ++风格。如果你确实改变了,你可以使用
ResizeDynamicArray(dynamicArray, capacity);
的上方。
这是我对函数声明的建议。我认为你可以相应地更新实现。
int* CreateDynamicArray(int capacity, int &size);
void DeleteDynamicArray(int*& dynamicArray, int &size);
void InsertElement(int*& dynamicArray, int element, int &size, int &capacity);
void DeleteElement(int* dynamicArray, int elementIndex, int &size);
void ResizeDynamicArray(int*& dynamicArray, int newCapacity);
void PrintArray(int *ptrArray, int capacity);
我注意到ResizeDynamicArray
中有一个错误。您正在使用:
int *newArray = new int[newCapacity];
for(int i = 0; i < newCapacity; i++) {
newArray[i] = (*dynamicArray)[i];
}
将值从旧数组复制到新数组。但是,旧数组没有新数组那么多的元素。您最终会从您不应该访问的内存中复制值。有必要将旧容量作为参数传递给函数,以便正确复制旧值。
答案 1 :(得分:0)
我相信我有解决方案,感谢R Sahu。
int* CreateDynamicArray(int capacity, int &size);
void DeleteDynamicArray(int* dynamicArray, int &size);
void InsertElement(int** dynamicArray, int element, int &size, int &capacity);
void DeleteElement(int** dynamicArray, int elementIndex, int &size);
void ResizeDynamicArray(int** dynamicArray, int newCapacity);
void PrintArray(int *ptrArray, int capacity);
#include <iostream>
int main()
{
int size = 0;
int capacity = 0;
int* ptrArray = CreateDynamicArray(capacity, size);
InsertElement(&ptrArray, 10, size, capacity);
InsertElement(&ptrArray, 20, size, capacity);
InsertElement(&ptrArray, 30, size, capacity);
InsertElement(&ptrArray, 40, size, capacity);
InsertElement(&ptrArray, 50, size, capacity);
PrintArray(ptrArray, size);
DeleteElement(&ptrArray, 1, size);
PrintArray(ptrArray, size);
DeleteElement(&ptrArray, 2, size);
PrintArray(ptrArray, size);
DeleteDynamicArray(ptrArray, size);
ptrArray = nullptr;
return 0;
}
int* CreateDynamicArray(int capacity, int &size)
{
size = 0;
return new int[capacity];
}
void DeleteDynamicArray(int* dynamicArray, int &size)
{
if (dynamicArray != nullptr) {
delete[] dynamicArray;
dynamicArray = nullptr;
}
size = 0;
}
void InsertElement(int** dynamicArray, int element, int &size, int &capacity)
{
if (size > capacity)
capacity += 2;
else
capacity += 1;
ResizeDynamicArray(dynamicArray, capacity);
(*dynamicArray)[size] = element;
size++;
}
void DeleteElement(int** dynamicArray, int elementIndex, int &size)
{
for (int i = elementIndex + 1; i < size; i++)
(*dynamicArray)[i - 1] = (*dynamicArray)[i];
size--;
}
void ResizeDynamicArray(int** dynamicArray, int newCapacity)
{
int *newArray = new int[newCapacity];
for (int i = 0; i < newCapacity; i++) {
newArray[i] = (*dynamicArray)[i];
}
if (*dynamicArray != nullptr)
delete[] * dynamicArray;
*dynamicArray = newArray;
}
void PrintArray(int *dynamicArray, int size)
{
for (int i = 0; i < size; i++) {
std::cout << dynamicArray[i] << " ";
}
std::cout << std::endl;
}