代码中的一个错误,写一个类是相同的指针数组
A bug in code about write a class that is same array of pointer?
大家好
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 to array[position]
const int * get(const int position) const;//is same to 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;
for (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();
for (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;
}
上面的代码应该打印
0
2
2
但输出是
9244616
9244600
2
,当我再次运行9244616
时,9244600
改变了。
问题是什么?
你的问题是
T *array[];
定义了一个没有元素的指针数组。然后尝试访问它的(不存在的)元素,这会导致未定义的行为。
另一个未定义的行为在析构函数 中delete[] array;
你应该只delete[]
那些new[]
-ed的东西。array
不是。相反,您应该遍历所有array
元素(如果有的话)并删除它们中的每一个。
最好的解决方案是使用std::vector
代替array。如果您坚持使用数组,将T* array[]
更改为T **array
,并在构造函数中为其分配内存
array = new T*[size];
问题是你在主目录中包含cpp文件并在那里实现,模板实现应该在h文件中完成。你的h文件应该是:
#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;
for (int i=0;i<size;i++)
{
array[i]=new T;
*array[i]=*points[i];
}
}
PointArray(const PointArray &pv)
{
nSize=pv.getSize();
for (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
代码仍然有点难,分配很糟糕(注意你在过程结束时得到堆断言)。但这对我很有效:022打印……欢呼。
(注意:)delete[] array;
是一个内存泄漏,因为你的对象不是一个数组,但你试图定义指向数组的指针,你应该把它改为
T** array
然后在数组第一个维度的析构函数循环中然后执行delete[] array