how to call destructor on some of the objects in a Dynamic Array

Dov

I finally got around to trying placement new to create an efficient dynamic array. the purpose is to understand how it works, not to replace class vector. The constructor works. A block is allocated but uninitialized. As each element is added, it is initialized. But I don't see how to use placement delete to call the destructor on only those elements that exist. Can anyone explain that one? This code works for allocating the elements one by one as the array grows, but the delete is not right.

template<typename T>
class DynArray {
private:
  uint32_t capacity;
  uint32_t size;
  T* data;
  void* operator new(size_t sz, T* place) {
    return place;
  }
  void operator delete(void* p, DynArray* place) {
  }

public:
  DynArray(uint32_t capacity) :
     capacity(capacity), size(0), data((T*)new char[capacity*sizeof(T)]) {}
  void add(const T& v) {
        new(data+size++) T(v);
  }
  ~DynArray() {
     for (int i = 0; i < size; i++)
       delete (this) &data[i];
     delete [] (char*)data;
  }
};
Stephen Newell

You actually found the only case (at least that I'm aware of) where you want to invoke the destructor manually:

  ~DynArray() {
     for (int i = 0; i < size; i++)
       data[i].~T();
     delete [] (char*)data;
  }

Combined with a trivial class and main, you should get the expected results:

struct S {
    ~S() { std::cout << __PRETTY_FUNCTION__ << '\n'; }
};

int main() {
    DynArray<S> da{10};
    da.add(S{});
    return 0;
}

Note that you see the destructor called twice since DynArray takes objects by const reference, thus it has a temporary.

$./a.out 
S::~S()
S::~S()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How are dynamic objects of a type with a deleted destructor constructed?

How to filter array by array of dynamic objects?

Is it OK not to call the destructor on placement new allocated objects?

How to filter an array of objects, based on some fields?

How to load some values to an instance of an array of objects?

How to remove some identical objects in array(js)?

How to write query for condition of some objects must NOT be in array of objects and one of some other objects Must be in array of object?

How to sort array of objects by dynamic proprety name?

How to make an array of objects from dynamic values?

How to access the dynamic key in an array of objects

JavaScript How to filter array of objects by dynamic key?

Dynamic allocation of class array with protected destructor

Destructor delete dynamic array declared in main

Pointer to array of objects require public destructor

Destructor on a Array of objects, that were already destructed

Javascript: How to call on different objects in array of arrays?

How to merge some properties in array of objects into another array?

From an array of dynamic objects, how to extract value of a mutltple property as array

How to explicitly call a namespace-qualified destructor?

How does C++ automatically call destructor?

How to understand call destructor method in CPP?

How to Manually call a Destructor on a Smart Pointer?

React how to make a call to an API and display an array in an array of objects PokeAPI

ExtJS: dateField - how to disable all and enable some dynamic array dates

Dynamic array of objects vs Dynamic array of pointers

How to verify if every object in an array of objects has some keys

How can I search the array with some of the letter/s of arrays objects?

Ramda how to filter array of objects where some key contains

How to expand subarray in array of objects and delete string some element