-
deleting class array
Hi, I've been able to create an array of class variables, but I can't get the program to delete them without an access violation.
Thanks!
PHP Code:
class TEST
{
public:
TEST(int y);
int x;
};
TEST::TEST(int y)
{
x=y;
}
class TEST2
{
public:
TEST *test[];
};
void main()
{
TEST2 t2;
for(int i=0; i<6;i++)
t2.test[i] = new(TEST)(i); // creating
// delete them here
}
-
What exactly are you tring to do?
I guess you would just do something like this:
Code:
for(i = 0; i < 6; i++)
delete t2.test[i];
Normally class members are destroyed in the classes Destructor... It is automatically called by 'delete'
Code:
class TEST
{
public:
TEST(void);
// This is the destructor.
~TEST(void);
};
TEST::~TEST(void)
{
for(int i = 0; i < 6; i++)
delete this->test[i];
}
-
Where the deletion is doesn't matter. And since the array is a member of TEST2 it certainly won't be in the TEST destructor.
But the core deletion code should work.
-
It still is giving an access violation when it tries to delete the pointer.
PHP Code:
TEST2::~TEST2()
{
for(int i=0; i<6;i++)
delete test[i];
}
I've tried how many different sorts of combinations with delete but whatever I try it keeps having an access violation.
-
Oh, and did I mention that [] is bad to use here anyway? This array has no lenght. You may not access it's elements because it has none. Declare test as
TEST *test[6];
-
I can't place
TEST *test[6];
because I want it to be a dynamic array, thats whats been giving me problems, I figured you would just start with
TEST *test[]; but trying to delete it won't work, so I don't know how to have the dynamic array and still delete it
-
Then I suggest you don't use an array of pointers but rather one pointer and use array new:
Code:
TEST *ar;
ar = new TEST[6](1);
// ...
delete[] ar;
Note that you can't pass individual arguments to the constructors anymore, only the same to each constructor. But this syntax is far easier to use. Else you'd have to use:
Code:
TEST **ar;
ar = new TEST*[6];
for(int i=0;i<6;++i)
ar[i] = new TEST(i);
// Access:
ar[3]->DoSomething();
// Deletion:
for(int i=0;i<6;++i)
delete ar[i];
delete[] ar;
Quite bad, and terribly prone to memory leaks.
-
what I've meant is instead of allocating 6 I want that to be a variable that gets increased everytime I call a function of mine. I've been able to get it to work with a fixed amount to begin with, but not when its a variable
-
you can allocate a variable amount of objects with new
-
But if that amount changes you have to reallocate the memory every time. In that case you should rather use a vector:
Code:
#include <vector>
using std::vector;
typedef vector<TEST> testvec;
testvec array;
// add a new element:
array.push_back(TEST(4));
No deletion needed, the vector does it all for you.
-
that works much better thank you! is there any tutorial that shows more about the vector, like how to go about shrinking the vector array and swapping values
-
I don't know if there is a tutorial for vectors, but a good reference should suffice. Look up vector in MSDN or on SGI's reference page.