[Gurobi560][C++]Why C++ examples don't have delete calls to free Gurobi objects ?

542 views
Skip to first unread message

Loc Do

unread,
Oct 20, 2013, 11:24:15 AM10/20/13
to gur...@googlegroups.com
Hi,

According to the Memory Management section in his reference manual for C++ (http://www.gurobi.com/documentation/5.6/reference-manual/cpp_reference_manual), we should delete GRBEnv or GRBModel objects when they are no longer in used.

However, when I look into the example codes, for instance, mip1_c++.cpp, I don't see any delete calls to free these objects. Is it safe to do so ?

The way the objects are created in the mip1_c++.cpp:

GRBEnv env = GRBEnv();

GRBModel model = GRBModel(env);


So creating these objects in this way, is it true that I don't need to delete them later ? I tried to delete them using:

delete env;

delete model;


However it prompted errors: "error: type ‘class GRBModel’ argument given to ‘delete’, expected pointer"

I also cannot delete any GRBVar objects although they wrote that "Some Gurobi methods return an array of objects or values. For example, GRBModel::addVars returns an array of GRBVar objects. It is the user's responsibility to free the returned array (using delete[]). The reference manual indicates when a method returns a heap-allocated result."

Please kindly advise. I am just new to C++ programming language.
 
Loc.


Sonja Mars

unread,
Oct 21, 2013, 8:30:28 AM10/21/13
to gur...@googlegroups.com
Hi,

Take a look at the example mip2_c++.cpp.
There environments and variables are created using:
GRBEnv *env = 0;
GRBVar *vars = 0;
env = new GRBEnv();
vars = model.getVars();

and deleted:
delete[] vars;
delete env;

Maybe you also want to take a look at this: https://en.wikipedia.org/wiki/Operator_delete

-- Sonja

Loc Do

unread,
Oct 21, 2013, 10:44:16 AM10/21/13
to gur...@googlegroups.com
Hi Sonja Mars,

Thanks for your reply.

Do you also notice that they didn't delete the GRBModel object ?

Following your wiki article leads me to the concept of "Resource Acquisition is Initialization". Since the GRBModel object is declared inside the try-catch statement, I guess it will be automatically freed up after the last line of code of the try-catch scope.

Sonja Mars

unread,
Oct 21, 2013, 11:46:23 AM10/21/13
to gur...@googlegroups.com
Hi,

These are the differences between objects and pointers. It is intentionally that some of the examples are written using the object directly and some using pointers. You might also want to look at params_c++.cpp. There we create a pointer to a GRBmodel and delete it.

-- Sonja
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Fan Yang

unread,
Oct 21, 2018, 8:35:11 PM10/21/18
to Gurobi Optimization
Hi Sonja,

For the same formulation, 

GRBEnv env = GRBEnv();
GRBModel model = GRBModel(env);

equal to

 GRBEnv* env = 0;
  GRBModel Model = 0;
    env = new GRBEnv();
    m = new GRBModel(*env)???

在 2013年10月21日星期一 UTC+2下午5:46:23,Sonja Mars写道:
Reply all
Reply to author
Forward
0 new messages