C++ destructor crashing program -


i've spent couple hours trying figure out why program misbehaving , haven't been able figure out yet.

result of running program

the copy constructor , assignment operator never called. either means isn't rule of 3 problem, or declared them incorrectly defaults being called. error mentions valid heap pointer block, maybe using 'new' keyword incorrectly?

let me know if more infmoratoin required me out, thanks.

main:

#include "ring.h" #include <iostream>  int main() {     ring<int> int_ring(3);     int_ring.add(1);     int_ring.add(2);     int_ring.add(3);     int_ring.add(4); // overwirte 1      (int = 0; < int_ring.size(); i++) {         std::cout << int_ring.get(i) << '\n';     }      return 0; } 

ring template class:

#include <iostream> #include <string>  template<class t> class ring { public:         ring(int size);     ring(const ring &other);     ~ring();         t* begin();     t* end();     ring& operator=(const ring rhs);     void add(t t);     t get(int i);     int size();  private:     int size_;     t *t_;     t *begin_;     t *end_;      void moveit() {         t_++;         if (t_ == end_) { t_ = begin_; }     }  };  template<class t> ring<t>::ring(int size) : size_(size), t_(new t[size_]), begin_(t_), end_(t_ + size_) { }  template<class t> ring<t>::ring(const ring &other) : size_(other.size_), t_(new t[size_]), begin_(t_), end_(t_ + size_) {     std::cout << "copy\n"; }  template<class t> t* ring<t>::begin() { return begin_; } template<class t> t* ring<t>::end() { return end_; }  template<class t> ring<t>& ring<t>::operator=(const ring<t> rhs) {     std::cout << "=\n";     std::swap(rhs);     return *this; }  template<class t> void ring<t>::add(t t) {     (*t_) = t;     moveit(); }  template<class t> t ring<t>::get(int i) {     return begin_[i]; }  template<class t> int ring<t>::size() {     return size_; }  template<class t> ring<t>::~ring() {     std::cout << "delete\n";     delete[] t_; } 

just figured out, deleting 't_' wasn't guaranteed pointing start of allocated block. had delete begin_ instead!


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -