dynamic - Delete[] array breaks my C++ program -


i have had issue while now. every time grow function calls delete line, program breaks. doesn't give me error besides has reached break point. have not found solution online during google searches. can help? update after hitting continue on error screen few times if came different error. states crtisvalidheappointer(puserdata)

driver.cpp

#include <iostream> #include "myvector.h"      using namespace std;  // printv function // used test copy constructor // parameter: myvector object void printv(myvector);  int main() {     cout << "\ncreating vector sam of size 4.";     myvector sam(4);      cout << "\npush 12 values vector.";     (int = 0; < 12; i++)         sam.push_back(i);      cout << "\nhere sam: ";     cout << sam;     cout << "\n---------------\n";      cout << "\ncreating vector joe of size 4.";     myvector joe(4);     cout << "\npush 6 values vector.";     (int = 0; < 6; i++)         joe.push_back(i * 3);      cout << "\nhere joe: ";     cout << joe;     cout << "\n---------------\n";      cout << "\ntest overloaded assignment operator \"joe = sam\": ";     joe = sam;      cout << "\nhere sam: ";     cout << sam;     cout << "\n---------------\n";      cout << "\nhere joe: ";     cout << joe;     cout << "\n---------------\n";      // pass copy of sam value     printv(sam);       cout << endl;     system("pause");     return 0; }  void printv(myvector v) {     cout << "\n--------------------\n";     cout << "printing copy of vector\n";     cout << v; } 

my vector.h

#include <iostream> #include <ostream> using namespace std; #pragma once   class myvector { private:     int vsize;     int vcapacity;     int* varray;     void grow();  public:     myvector();     myvector(int n);     myvector(const myvector& b);     int size() const;     int capacity() const;     void clear();     void push_back(int n);     int& at(int n) const;     myvector& operator=(const myvector& rho);     ~myvector(); };  ostream& operator<<(ostream& out, const myvector& rho); 

myvector.cpp

#include "myvector.h" #include <iostream> #include <ostream> using namespace std;   myvector::myvector() {     varray = nullptr;     vsize = 0;     vcapacity = 0; }  myvector::myvector(int n) {     varray = new int[vcapacity];     vsize = 0;     vcapacity = n; }  myvector::myvector(const myvector& b) {     vsize = b.size();     varray = new int[vsize];     (int = 0; < b.size(); i++)     {         this->varray[i] = b.varray[i];     } }  int myvector::size() const {     return vsize; }  int myvector::capacity() const {     return vcapacity; }  void myvector::clear(void) {     if (varray != nullptr)     {         delete[] varray;         varray = nullptr;         vsize = 0;         vcapacity = 0;     } }  void myvector::push_back(int n) {     if (vcapacity == 0)     {         vcapacity++;         int* tmp = new int[vcapacity];         delete[] varray;         varray = tmp;     }      if (vsize >= vcapacity)     {         grow();     }     varray[vsize] = n;     vsize++; }  void myvector::grow() {     vcapacity = vcapacity + vcapacity;     int* tmp = new int[vcapacity];     (int = 0; < vsize+1; i++)     {         tmp[i] = varray[i];     }     delete[] varray;     varray = tmp; }  int& myvector::at(int index) const {     if (index >= 0 && index<vsize)     {         return varray[index];     }     else     {         throw index;     } }  myvector& myvector::operator=(const myvector& rho) {     // test self assignment     if (this == &rho)         return *this;      // clean array in left hand object (this)     delete[] this->varray;      // create new array big enough hold right hand object's data     vsize = rho.size();     this->varray = new int[vsize];      // copy data     (int = 0; < rho.size(); i++)     {         this->varray[i] = rho.varray[i];     }      // return object     return *this; }  myvector::~myvector() {     if (varray != nullptr)     {         clear();     } }  ostream& operator<< (ostream& out, const myvector& rho) {     (int = 0; < rho.size(); i++)     {         out << rho.at(i);     }     return out; } 

your problem constructor takes parameter:

myvector::myvector(int n) {     varray = new int[vcapacity];     vsize = 0;     vcapacity = n; } 

you using vcapacity before assign value. can lead attempt allocate large or not large enough block of data.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -