将动态数组转移到新的动态数组,删除充满动态对象的动态数组
Transfer Dynamic Array to new Dynamic Array, Delete Dynamic array filled with Dynamic Objects
如果我创建一个 Zebras 的动态数组,然后说,我创建了一个新的临时数组,我想在其中传输原始数组中的对象。
然后我将指针从原始 Array 移动到 Temp。我需要在 move array = temp 之前删除旧的数组项吗?
此外,我似乎泄漏了但不确定为什么,我似乎同时删除了数组和指针。
class Zebra {
public:
//int age = 0;
};
int main() {
Zebra** array = new Zebra * [10];
for (int i = 0; i < 10; i++) {
array[i] = new Zebra;
}
Zebra** temp = new Zebra * [20];
for (int i = 0; i < 10; i++) {
temp[i] = array[i];
}
// do i need to delete the old array items before move array = temp?
array = temp;
delete[]temp;
temp=NULL;
for (int i = 0; i < 10; i++) {
delete[]array[i];
array[i]=NULL;
}
delete[]array;
array= NULL;
}
你有你要向后删除的东西,你想要:
delete[] array;
array = nullptr;
array = temp;
for (int i = 0; i < 10; i++) {
delete array[i];
array[i] = NULL;
}
delete[] array;
array = NULL;
请注意,您还错误地使用了 delete[]
,其中应单独使用 delete
来释放 array[i]
元素。
为什么这样工作?
最初,您为指针 array
分配创建了一个内存块,其中包含十个指向 Zebra
的指针。然后,当您创建 array
两倍大的 temp
并将 array
中的每个指针分配给 temp
时,您的 temp
保存每个 [=15] 的起始地址=](其中 0 <= i < 10
)。
您所做的只是 delete[] array
,它释放了包含 10
指向 Zebra
的原始存储空间的内存块。当您分配 array = temp;
现在数组指向 20
指针的新内存块 Zebra
.
所以在你的重新分配方案中,你刚刚创建了一个更大的指针块,分配了从 array
到 temp
的所有指针,删除了原来的指针块,并分配了新的指针块到 array
(完成)。 (本质上是做 realloc()
在 C 中做的事情)
完整的例子是:
#include <iostream>
class Zebra {
public:
//int age = 0;
};
int main() {
Zebra **array = new Zebra*[10];
for (int i = 0; i < 10; i++) {
array[i] = new Zebra;
}
Zebra **temp = new Zebra*[20];
for (int i = 0; i < 10; i++) {
temp[i] = array[i];
}
delete[] array;
array = nullptr;
array = temp;
for (int i = 0; i < 10; i++) {
delete array[i];
array[i] = nullptr;
}
delete[] array;
array = nullptr;
}
内存Use/Error检查
valgrind ./bin/zebra
==22402== Memcheck, a memory error detector
==22402== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==22402== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==22402== Command: ./bin/zebra
==22402==
==22402==
==22402== HEAP SUMMARY:
==22402== in use at exit: 0 bytes in 0 blocks
==22402== total heap usage: 13 allocs, 13 frees, 72,954 bytes allocated
==22402==
==22402== All heap blocks were freed -- no leaks are possible
==22402==
==22402== For counts of detected and suppressed errors, rerun with: -v
==22402== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
如果我创建一个 Zebras 的动态数组,然后说,我创建了一个新的临时数组,我想在其中传输原始数组中的对象。
然后我将指针从原始 Array 移动到 Temp。我需要在 move array = temp 之前删除旧的数组项吗?
此外,我似乎泄漏了但不确定为什么,我似乎同时删除了数组和指针。
class Zebra {
public:
//int age = 0;
};
int main() {
Zebra** array = new Zebra * [10];
for (int i = 0; i < 10; i++) {
array[i] = new Zebra;
}
Zebra** temp = new Zebra * [20];
for (int i = 0; i < 10; i++) {
temp[i] = array[i];
}
// do i need to delete the old array items before move array = temp?
array = temp;
delete[]temp;
temp=NULL;
for (int i = 0; i < 10; i++) {
delete[]array[i];
array[i]=NULL;
}
delete[]array;
array= NULL;
}
你有你要向后删除的东西,你想要:
delete[] array;
array = nullptr;
array = temp;
for (int i = 0; i < 10; i++) {
delete array[i];
array[i] = NULL;
}
delete[] array;
array = NULL;
请注意,您还错误地使用了 delete[]
,其中应单独使用 delete
来释放 array[i]
元素。
为什么这样工作?
最初,您为指针 array
分配创建了一个内存块,其中包含十个指向 Zebra
的指针。然后,当您创建 array
两倍大的 temp
并将 array
中的每个指针分配给 temp
时,您的 temp
保存每个 [=15] 的起始地址=](其中 0 <= i < 10
)。
您所做的只是 delete[] array
,它释放了包含 10
指向 Zebra
的原始存储空间的内存块。当您分配 array = temp;
现在数组指向 20
指针的新内存块 Zebra
.
所以在你的重新分配方案中,你刚刚创建了一个更大的指针块,分配了从 array
到 temp
的所有指针,删除了原来的指针块,并分配了新的指针块到 array
(完成)。 (本质上是做 realloc()
在 C 中做的事情)
完整的例子是:
#include <iostream>
class Zebra {
public:
//int age = 0;
};
int main() {
Zebra **array = new Zebra*[10];
for (int i = 0; i < 10; i++) {
array[i] = new Zebra;
}
Zebra **temp = new Zebra*[20];
for (int i = 0; i < 10; i++) {
temp[i] = array[i];
}
delete[] array;
array = nullptr;
array = temp;
for (int i = 0; i < 10; i++) {
delete array[i];
array[i] = nullptr;
}
delete[] array;
array = nullptr;
}
内存Use/Error检查
valgrind ./bin/zebra
==22402== Memcheck, a memory error detector
==22402== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==22402== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==22402== Command: ./bin/zebra
==22402==
==22402==
==22402== HEAP SUMMARY:
==22402== in use at exit: 0 bytes in 0 blocks
==22402== total heap usage: 13 allocs, 13 frees, 72,954 bytes allocated
==22402==
==22402== All heap blocks were freed -- no leaks are possible
==22402==
==22402== For counts of detected and suppressed errors, rerun with: -v
==22402== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)