free a pointer object
Mar 19, 2010 at 1:51pm UTC
Hi,
Suppose I have a class as shown below (I know it is very basic, no constructor etc..).
1 2 3 4 5 6 7 8 9 10 11 12 13
template <class T>
class bigEndian {
private :
vector<T> fData;
public :
vector<T> getData(); // return STL vector
T getData(int index); // overloaded member function
void display() const ; // print output
void convert(int interval, ifstream &filePtr); // convert binary to int/double (template)
};
Now if I create an object with datatype int as,
1 2 3 4 5 6 7 8
bigEndian<int > *ix[10];
for (int j = 0; j < 10; j++) {
// allocate
ix[j] = new bigEndian<int >;
...
...
// some operations
}
Now, how do I free this object? I know this is not the most efficient method to go about, but just to know.
thank you!
Mar 19, 2010 at 1:54pm UTC
There is a simple rule. For each new that you execute, you have to do a delete. And for each new[] you have to use delete[]. Nothing else. So use it like this:
1 2 3
ix[j] = new bigEndian<int >;
...
delete ix[j];
if you use instead
1 2 3
ix = new bigEndian<int > [10];
...
delete [] ix;
understood?
Maikel
Mar 19, 2010 at 1:56pm UTC
Great! thank you very much
Topic archived. No new replies allowed.