I have allocated an array of object as:
int numPlwDistanceClimb = 5;
int numFl = 6;
GRBVar** t_gd = 0;
t_gd = new GRBVar* [numFl];
GRBVar* x = 0;
x = model.addVars(numFl, GRB_CONTINUOUS);
for (int k = 0; k < numFl; k++)
{
t_gd[k] = model.addVars(numPlwDistanceClimb, GRB_CONTINUOUS);
}
I delete the arrays as follows but it does not work.
delete x;
for (int i = 0; i < numFl; ++i)
{
delete t_gd[i];
}
delete [] t_gd;
Can anyone help me? Thank you in advance
I have allocated an array of object as:
No you have allocated array of pointers to objects. Which means you have to allocate every pointer in that array if you want to use it properly, otherwise attempt to use them would cause undefined behavior.
GRBVar** t_gd = new GRBVar* [numFl];
for (int n = 0; n < numFl; n++)
{
t_gd[i] = new GRBVar;
// Or set them to nullptr
}
Also if you need array of pointers in C++, you could use std::vector
std::vector<GRBVar *> t_gd;
Where you dont have to allocate, free resources and you have way more possibilities.
Also you can use smart pointers
std::vector< std::unique_ptr< GRBVar > > t_gd;