Problems with delete in destructor
        Posted  
        
            by Vera
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Vera
        
        
        
        Published on 2010-04-02T21:14:40Z
        Indexed on 
            2010/04/02
            21:23 UTC
        
        
        Read the original article
        Hit count: 317
        
c++
Hello, I wrote this code.
The constructor works normally, but in the destructor I get "Windows has triggered a breakpoint." How should I correct this?
template class CyclicalArray { 
private: 
   T* mem_ptr;
public: 
   CyclicalArray(size_t capacity, const T& default_value) {
   this->default_value = default_value; 
   this->capacity = capacity; 
   head_index = 0; 
   mem_ptr = ::new T[capacity]; //memory allocating 
   for(T* p = mem_ptr; p < mem_ptr + capacity * sizeof(T); p += sizeof(T)) { 
       ::new (p) T (default_value); //initialization 
   } 
} 
~CyclicalArray() { 
   for(T* p = mem_ptr + sizeof(T); p < mem_ptr + capacity * sizeof(T); p += sizeof(T)) { 
      p->~T();
   } 
   delete[] mem_ptr; 
}
© Stack Overflow or respective owner