c++ - How to properly manage a vector of void pointers -


first, background:

i'm working on project requires me simulate interactions between objects can thought of polygons (usually triangles or quadrilaterals, fewer 7 sides), each side of composed of radius of 2 circles variable (and possibly zero) number of 'rivers' of various constant widths passing between them, , out of polygon through other side. these rivers , circles , widths (and positions of circles) specified @ runtime, 1 of these polygons n sides , m rivers running through can described array of n+2m pointers, each referring relevant rivers/circles, starting arbitrary corner of polygon , passing around (in principal, since rivers can't overlap, should specifiable less data, in practice i'm not sure how implement that).

i programming in python, found more complex arrangements performance unacceptably slow. in porting on c++ (chosen because of portability , compatibility sdl, i'm using render result once optimization complete) @ of loss how deal polygon structure.

the obvious thing make class them, c++ lacks runtime-sized arrays or multi-type arrays, way ludicrously cumbersome set of vectors describing list of circles, rivers, , relative placement, or else more cumbersome 'edge' class of kind. rather this, seems better option use simpler, though still annoying, vector of void pointers, each pointing rivers/circles described above.

now, question:

if correct, proper way handle relevant memory allocations here minimum amount of confusion (not saying much...) this:

int dostuffwithpolygons(){     std::vector<std::vector<void *>> polygons;     while(/*some circles aren't assigned polygon*/){         std::vector<void *> polygon;         void *start = &/*next circle has not yet been assigned polygon*/;         void *lastcircle = start;         void *nextcircle;         nextcircle = &/*next circle put polygon*/;         while(nextcircle != start){             polygon.push_back(lastcircle);             std::vector<river *> rivers = /*list of rivers between last circle , next circle*/;             for(unsigned = 0; < rivers.size(); i++){                 polygon.push_back(rivers[i]);             }             lastcircle = nextcircle;             nextcircle = &/*next circle put polygon*/;         }         polygons.push_back(polygon);     }      int score = 0;     //do whatever you're going evaluate polygons here     return score; }  int main(){     int bestscore = 0;     std::vector<int> bestarrangement; //contains position of each circle     std::vector<int> currentarrangement = /*whatever arbitrary starting arrangement appropriate*/;     while(/*not done evaluating polygon configurations*/){         //fiddle current arrangement bit         int currentscore = dostuffwithpolygons();         if(currentscore > bestscore){             bestscore = currentscore;             bestarrangement = currentarrangement;         }     }      //somehow report best arrangement      return 0; } 

if understand how stuff handled, shouldn't need delete or .clear() calls because goes out of scope after function call. correct this? also, there part of above needlessly complex, or else insufficiently complex? right in thinking simple c++ let me make it, or there way avoid of roundabout construction?

and if you're response going 'don't use void pointers' or 'just make polygon class', unless can explain how make problem simpler, save trouble. 1 ever see code, don't care adhering best practices. if forget how/why did , causes me problems later, that's own fault insufficiently documenting it, not reason have written differently.

edit since @ least 1 person asked, here's original python, handling polygon creation/evaluation part of process:

#lots of setup stuff, such circle , river classes  def evaluatearrangement(circles, rivers, tree, arrangement): #circles, rivers contain circles, rivers placed. tree class describing rivers go between circles, unrelated problem @ hand. arrangement contains (x,y) position of circles in current arrangement.     polygons = []     unassignedcircles = range(len(circles))     while unassignedcircles:         polygon = []         start = unassignedcircles[0]         lastcircle = start         lastlastcircle = start         nextcircle = getnearest(start,arrangement)         unassignedcircles.pop(start)         unassignedcircles.pop(nextcircle)         while(not nextcircle = start):             polygon += [lastcircle]             polygon += getriversbetween(tree, lastcircle,nextcircle)             lastlastcircle = lastcircle             lastcircle = nextcircle;             nextcircle = getnearest(lastcircle,arrangement,lastlastcircle) #the last argument here guarantees new nextcircle not same last lastcircle, otherwise have been guaranteed be.             unassignedcircles.pop(nextcircle)         polygons += [polygon]     return evaluatepolygons(polygons,circles,rivers) #defined outside. 

void template argument must lower case. other should work, recommend using base class that. smart pointer can let system handle memory management.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -