C++ API Gurobi 6.5: Removed the model assignment operator

35 views
Skip to first unread message

Nikos Lappas

unread,
Nov 20, 2015, 1:37:49 PM11/20/15
to Gurobi Optimization
Hi,

I noticed that the capability of using the assignment operator in the model class was removed (we cannot anymore write modelA = modelB). Is there any particular reason behind this choice?

Thanks,

Nick

Renan Garcia

unread,
Nov 20, 2015, 4:21:57 PM11/20/15
to gur...@googlegroups.com
The assignment operator in the C++ API was forbidden for the latest 6.5 release. In previous versions, the assignment operator was not explicitly declared, which means a default version of it would be provided by the compiler (see https://en.wikipedia.org/wiki/Assignment_operator_(C%2B%2B) for more details).

However, the default version can lead to a crash because it only performs a shallow copy of the underlying pointer to the model rather than a deep copy of the model itself. Calling the destructor for one of the copies frees the underlying pointer and leaves the other copy with a pointer that is no longer invalid. See https://groups.google.com/forum/#!topic/gurobi/FszoX0-Tx1I for an example of where this was leading to a crash. 

If you need a deep copy, use the copy constructor (see http://www.gurobi.com/documentation/6.5/refman/cpp_grbmodel2.html). For example,

  GRBModel modelB(modelA);
  // or
  GRBModel modelC = modelA;

If you need a shallow copy, use a reference or a pointer. For example,

  GRBModel &modelD = modelA;
  // or
  GRBModel *modelE = &modelA;

--

---
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/d/optout.

Reply all
Reply to author
Forward
0 new messages