c++ - A bug in code about write a class that is same array of pointer? -
hello have below code
in pointarray.h
#ifndef pointarray_h_included #define pointarray_h_included template <typename t> class pointarray { private: int nsize; t *array[]; public: pointarray(); pointarray(const t *points[],const int size); pointarray(const pointarray &pv); ~pointarray(); int * get(const int position);//is same array[position] const int * get(const int position) const;//is same array[position] const int getsize() const;//get size of array }; #endif // pointarray_h_included
in pointarray.cpp
#include "pointarray.h" #include<iostream> #include <assert.h> using namespace std; template<class t> pointarray<t>::pointarray(const t *points[],const int size) { nsize=size; (int i=0;i<size;i++) { array[i]=new t; *array[i]=*points[i]; } } template<class t> pointarray<t>::pointarray() { nsize=0; } template<class t> pointarray<t>::pointarray(const pointarray &pv) { nsize=pv.getsize(); (int i=0;i<nsize;i++) { array[i]=new t; *array[i]=*(pv.get(i)); } } template<class t> const int pointarray<t>::getsize() const { return nsize; } template<class t> pointarray<t>::~pointarray() { delete[] array; nsize=0; } template<class t> int * pointarray<t>::get(const int position) { assert(position>-1 && position<nsize); return array[position]; } template<class t> const int * pointarray<t>::get(const int position) const { return array[position]; }
in main.cpp
#include<iostream> #include "pointarray.h" #include "pointarray.cpp" using namespace std; int main() { int x=22; int y=3; const int * a[2]={&x,&y}; pointarray<int> p; pointarray<int> p2(a,2); pointarray<int> p3(p2); cout << p.getsize() << endl; cout << p2.getsize() << endl; cout << p3.getsize() << endl; return 0; }
above code should print
0 2 2
but output
9244616 9244600 2
and when run again 9244616
, 9244600
changed.
problem?
the problem including cpp file in main , implementing there, template implementations should done in h files. h file should be:
#ifndef pointarray_h_included #define pointarray_h_included template <typename t> class pointarray { private: int nsize; t *array[]; public: pointarray() { nsize=0; } pointarray(const t *points[],const int size) { nsize=size; (int i=0;i<size;i++) { array[i]=new t; *array[i]=*points[i]; } } pointarray(const pointarray &pv) { nsize=pv.getsize(); (int i=0;i<nsize;i++) { array[i]=new t; *array[i]=*(pv.get(i)); } } ~pointarray() { delete[] array; nsize=0; } int * get(const int position) { assert(position>-1 && position<nsize); return array[position]; } const int * get(const int position) const { return array[position]; } const int getsize() const { return nsize;} }; #endif // pointarray_h_included
code still kind of hard , allocations bad(notice heap assert @ end of process).. worked me: 0 2 2 printed... cheers.
(notice that:)
delete[] array;
is memory leak, since object not array per try define pointers arrays, should change
t** array
and @ destructor loop on array first dimension , delete[] array
Comments
Post a Comment