From: Matthew Fernandez <matthew.fernandez@gmail.com> Date: Thu, 18 Feb 2021 05:15:54 +0000 (-0800) Subject: remove manual memory management of rectangle array X-Git-Tag: 2.47.0~40^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ea51a12f0c076a9f5c47d0866fd01d7731214aa1;p=graphviz remove manual memory management of rectangle array This unifies some Windows and non-Windows code, removes some unwise VLAs, and removes the need to manually deallocate this array, making the code less error prone. We could go further with removing manual allocations, but for now this is a minimal net improvement. --- diff --git a/lib/vpsc/csolve_VPSC.cpp b/lib/vpsc/csolve_VPSC.cpp index 482c16ce0..72b5c3913 100644 --- a/lib/vpsc/csolve_VPSC.cpp +++ b/lib/vpsc/csolve_VPSC.cpp @@ -23,6 +23,7 @@ #include <vpsc/solve_VPSC.h> #include <cstdlib> #include <cassert> +#include <vector> #include <vpsc/csolve_VPSC.h> Variable* newVariable(int id, double desiredPos, double weight) { return new Variable(id,desiredPos,weight); @@ -38,39 +39,25 @@ VPSC* newIncVPSC(int n, Variable* vs[], int m, Constraint* cs[]) { } int genXConstraints(int n, boxf* bb, Variable** vs, Constraint*** cs,int transitiveClosure) { -#ifdef _WIN32 - Rectangle** rs = new Rectangle* [n]; -#else - Rectangle* rs[n]; -#endif + std::vector<Rectangle*> rs(n); for(int i=0;i<n;i++) { rs[i]=new Rectangle(bb[i].LL.x,bb[i].UR.x,bb[i].LL.y,bb[i].UR.y); } - int m = generateXConstraints(n,rs,vs,*cs,transitiveClosure?true:false); + int m = generateXConstraints(n,rs.data(),vs,*cs,transitiveClosure?true:false); for(int i=0;i<n;i++) { delete rs[i]; } -#ifdef _WIN32 - delete [] rs; -#endif return m; } int genYConstraints(int n, boxf* bb, Variable** vs, Constraint*** cs) { -#ifdef _WIN32 - Rectangle** rs = new Rectangle* [n]; -#else - Rectangle* rs[n]; -#endif + std::vector<Rectangle*> rs(n); for(int i=0;i<n;i++) { rs[i]=new Rectangle(bb[i].LL.x,bb[i].UR.x,bb[i].LL.y,bb[i].UR.y); } - int m = generateYConstraints(n,rs,vs,*cs); + int m = generateYConstraints(n,rs.data(),vs,*cs); for(int i=0;i<n;i++) { delete rs[i]; } -#ifdef _WIN32 - delete [] rs; -#endif return m; }