2

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

13
  • 3
    Use std::vector instead and all your problems will be handled. Commented Aug 24, 2017 at 12:54
  • you dont initialize every t_gd individually in this code Commented Aug 24, 2017 at 12:55
  • I don't see any code where you actually write into t_gd. If you call free on uninitialized pointers a crash is what you can expect. Commented Aug 24, 2017 at 12:56
  • I initialize t_gd now. But it does not work !! Commented Aug 24, 2017 at 13:14
  • 1
    @ThomasEdison it works but does not free all variables -- So did the API state that those addresses are allocated using malloc, calloc, or realloc? I think you're using the API incorrectly and overcomplicated your program using pointers. I am assuming that the authors of the C++ interface would properly implement the objects being returned in that there is no need for you to be manually releasing memory. The addVars function returns an object -- why not just use std::vector<GRBVar> t_gd(numF); and fill the vector with the return value of addVars? Commented Aug 24, 2017 at 13:41

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

I miss some detail in my code last time. Please review it. Thanks
If addVars returns pointer to allocated memory it should be OK. Could you provide us this function please?
1

You must at least put every element of t_gc to NULL, otherwise the array contains random values and when you delete those it will crash

GRBVar** t_gd = 0;
t_gd = new GRBVar* [numFl];

for (int i = 0; i < numFl; i++)
{
  t_gd[i] = NULL;
}

3 Comments

I miss some detail in my code last time. Please review it. Thanks
nulling should not be neccessary since any entry will be filled within the loop
How does model.addVars(numPlwDistanceClimb, GRB_CONTINUOUS); create a pointer to GRBVar? With new or something else?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.