The following has some memory allocation error. When I try to add the first entry it has no prob, but on further additions some of the element cannot be accessed (are not saved with their actual values...)
template <class T>
class List
{
public:
List(); // <-- Constructor
int add(T rec);
~List();
T *data;
int n;
};
template<class T>
int List<T>::add(T rec)
{
if (n==0) {
data = (T*) malloc(sizeof(T));
if (data==NULL) return FALSE;
n=1;
data[n-1] = rec;
}
else {
data = (T*) realloc(data,sizeof(T)*n+1);
if (data==NULL) return FALSE;
n++;
data[n-1] = rec;
}
return TRUE;
}
template<class T>
List<T>::List()
{
n=0;
}
template<class T>
List<T>::~List()
{
if (n!=0) free((void*)data);
}