我一直在尝试为实践编写自己的STL容器实现,但我在释放元素时遇到了一些问题。我已经创建了一个简单的Array类,它基本上是标准C++数组的包装器。我一直在尝试实现的一个大变化是,如果数组没有默认构造函数,就允许它们被初始化(我知道Vectors可以做到这一点,但我想练习实现它)。由于这个特性,我不能使用new,所以我决定让容器像标准的STL容器一样使用分配器。Array看起来有点像这样:
template<class T, class A = std::allocator<T>> class Array {
public:
// STL definitions and iterators...
/// initializes an array of size elements with all elements being
/// default constructed.
Array(const size_type &size) : Array(size, T()) {
}
/// Initializes an array of size elements with all elements being
/// copies of the fill element.
Array(const size_type &size, const T &fill) {
this->allocator = A(); // Get allocator from template
this->size = this->max_size = size;
// Allocate data array and copy the fill element into each
// index of the array.
this->data = this->allocator.allocate(size);
this->allocator.construct(this->data, fill);
}
/// Deletes the array and all of its elements.
~Array() {
// deallocate using the allocator
this->allocator.deallocate(this->data, this->size);
}
// other things...
}为了测试我的数组,我创建了一个简单的测试类,它简单地跟踪它存在的实例的数量,每次调用构造函数或复制构造函数时,名为instance_count的变量就会递增,每次调用析构函数时,变量就会递减。然后,我编写了以下方法来断言Array正确地创建和销毁了元素:
void testArray() {
for (int i = 1; i < 100; i++) {
std::cout << TestObject::instance_count << ", "; // should always == 0
Array<TestObject> testArray(i); // Create array of I elements
std::cout << TestObject::instance_count << ", "; // should == i
}
}我的预期输出是0, 1, 0, 2, 0, 3, 0, 4...,这意味着在作用域开始时不存在TestObjects,然后在数组中分配正确的数量,并在作用域结束时销毁它们。相反,我得到了0, 1, 1, 2, 2, 3, 3, 4, 4...的输出,它表明由于某种原因,元素没有被正确销毁。这就像只有在分配新元素时才释放元素,但这不是我想要的行为。此外,在for循环之外,instance_count等于100,这意味着即使在没有更多的Array实例之后,仍有剩余的对象。有人能给我解释一下为什么std::allocator没有正确地清理元素吗?
发布于 2017-01-31 06:16:03
因为您不是在销毁对象,而是释放它们占用的内存。分配器将分配/释放(使用allocate和deallocate)和构造/销毁(使用construct和destroy)的概念分开。
要创建对象,需要调用allocate和construct。
要销毁对象,需要先调用destroy,然后调用deallocate。
https://stackoverflow.com/questions/41945921
复制相似问题