By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,677 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,677 IT Pros & Developers. It's quick & easy.

C++ vectors ...encountered a problem and need to close

P: 14
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated monomers) which are represented as doubly pointed noncomplete binary trees. There are three different types of monomers (hence the three different constructer calling functions) the first one is the "leaves" of the tree, the second adds length between branch points and the third actually branches the tree (making it binary). There are two vectors which hold the reactive species, they are vectors of pointers which only hold the pointer to the last element in the tree (i.e. the last reactive element of the polymer). At the end of the program one of the vectors is empty, and the other one holds the pointers to the last monomer of each polymer formed. The program works most of the time, but when i run it with large numbers of monomers it fails by saying "polymer44.exe has encountered a problem and needs to close. We are sorry for the inconvenience." and gives me the option to debug, or send microsoft an error report. I've spent a lot of debugging time just to realize that this is probably a problem having to do with my vector "shuffling", or my use of new to create the monomers in the first place. so here is a basic outline of what is happening. any suggestions are welcome and I appreciate your time and effort greatly. :)

Expand|Select|Wrap|Line Numbers
  1.  
  2. //initialize the queue to hold pointers to the polymer chain ends
  3. vector<monomer*> nucleophiles; 
  4. //initialize the queue to hold pointer to the reactive monomers
  5. vector<monomer*> electrophiles;
  6.  
  7. //function to dynamically allocate memory for the "leaf" monomers
  8. void initializechains(vector<monomer*>& nucs, int& chainum, int& namenum)
  9. {
  10.     if(chainum > 0)
  11.     {
  12.         for(int i=0; i< chainum; i++)
  13.         {    
  14.         //pointers for the initialization of the correct monomer
  15.         monomer* pointforward = NULL;
  16.  
  17.         //creating and initializing the monomer
  18.         monomer* monomeri = new monomer(pointforward, namenum);
  19.         monomeri->namenumber = namenum; 
  20.  
  21.         //inserting the monomer onto its vector
  22.         nucs.insert(nucs.begin(), monomeri);
  23.  
  24.         //deleting pointers
  25.         delete pointforward;
  26.         monomeri = NULL;
  27.  
  28.         //resetting namenumber
  29.         namenum = namenum - 1;
  30.         }
  31.  
  32.     }
  33.     else
  34.     {
  35.         cout << "there are no original chains to initialize." <<endl;
  36.     }
  37.  
  38. }
  39.  
  40. //same thing but for the additive monomers
  41. void initializeaddmons(vector<monomer*>& elecs, int& adds, int& namenum)
  42. {
  43.     if(adds > 0)
  44.     {
  45.         for(int i =0; i<adds; i++)
  46.         {
  47.         //pointers for the initialization of the correct monomer
  48.         monomer* pointforward = NULL;
  49.         monomer* pointbackward = NULL;
  50.  
  51.         //creating and initializing the monomer
  52.         monomer* addmonomeri = new monomer(pointforward, pointbackward);
  53.         addmonomeri->namenumber = namenum;
  54.  
  55.         //inserting the monomer onto its vector
  56.         elecs.insert(elecs.begin(), addmonomeri);
  57.  
  58.         //deleting pointers
  59.         delete pointforward;
  60.         delete pointbackward;
  61.         addmonomeri = NULL;
  62.  
  63.         //resetting namenumber
  64.         namenum = namenum + 1;
  65.         }
  66.     }
  67.     else
  68.     {
  69.         //cout << "there are no additive monomers to initialize." <<endl;
  70.     }
  71.  
  72. }
  73.  
  74. //same thing again but for the branching monomers
  75. void initializebranchmons(vector<monomer*>& elecs, int& branchers, int& namenum)
  76. {
  77.     if(branchers > 0)
  78.     {
  79.         for(int i = 0; i<branchers; i++)
  80.         {
  81.         //pointers for the initialization of the correct monomer
  82.         monomer* pointforward = NULL;
  83.         monomer* pointbackward = NULL;
  84.         monomer* pointbranch = NULL;
  85.  
  86.         //creating and initializing the monomer
  87.         monomer* branchmoni = new monomer(pointforward, pointbackward, pointbranch);
  88.         branchmoni->namenumber = namenum;
  89.  
  90.         //inserting the monomer onto its vector
  91.         elecs.insert(elecs.begin(), branchmoni);
  92.  
  93.         //deleting pointers
  94.         delete pointforward;
  95.         delete pointbackward;
  96.         delete pointbranch;
  97.         branchmoni = NULL;
  98.  
  99.         //resetting namenumber
  100.         namenum = namenum + 1;
  101.         }
  102.     }
  103.     else
  104.     {
  105.         cout << "there are no branching monomers to initialize." <<endl;
  106.     }
  107.  
  108. }
  109.  
  110. void addmonomer(vector<monomer*>& nucs, vector<monomer*>& elecs, int& nucount, int& elecount, double addrate_4, double branchrate1_4, double branchrate2_4, double alpha_6, int iter3)
  111. {
  112.     //checks
  113.     //int elesizecheck = elecs.size();
  114.     //int nucsizecheck = nucs.size();
  115.  
  116.     //declare the iterators
  117.     vector<monomer*>::iterator nuchooser = nucs.begin();
  118.     vector<monomer*>::iterator elechooser = elecs.begin();
  119.  
  120.     //set the iterators
  121.     for(int nucount2 = 0; nucount2 != nucount; ++nucount2)
  122.     {
  123.         ++nuchooser;
  124.     }
  125.  
  126.     for(int elecount2 = 0; elecount2 != elecount; ++elecount2)
  127.     {
  128.         ++elechooser;
  129.     }
  130.  
  131.     //see if the nucleophile and electrophile are in the same polymer
  132.     bool loopy = 0;
  133.     looper(nucs[nucount], loopy, elecs[elecount]->namenumber);
  134.     if(elecs.size()==1 && nucs.size()==1 && (elecs[elecount]->namenumber == nucs[nucount]->namenumber || loopy ==1))
  135.     {
  136.         nucs.erase(nuchooser);
  137.     }
  138.     else
  139.     {
  140.  
  141.     //protection against a monomer reacting with itself
  142.     if(nucs.size()==1 && elecs.size()>1 && (elecs[elecount]->namenumber == nucs[nucount]->namenumber || loopy ==1))
  143.     {
  144.         while(elecs[elecount]->namenumber == nucs[nucount]->namenumber)
  145.         {
  146.             determine_electrophile(elecs, elecount, addrate_4, branchrate1_4, branchrate2_4, alpha_6);
  147.         }
  148.  
  149.         //reset the electrophile iterator
  150.         elechooser = elecs.begin();
  151.  
  152.         for(int elecount2 = 0; elecount2 != elecount; ++elecount2)
  153.         {
  154.             ++elechooser;
  155.         }
  156.     }
  157.     else if(elecs.size()==1 && nucs.size()>1 && (elecs[elecount]->namenumber == nucs[nucount]->namenumber || loopy ==1))
  158.     {
  159.         while(elecs[elecount]->namenumber == nucs[nucount]->namenumber)
  160.         {
  161.             determine_nucleophile(nucs, nucount, alpha_6, iter3);
  162.         }
  163.  
  164.         //reset the nucleophile iterator
  165.         nuchooser = nucs.begin();
  166.  
  167.         for(int nucount2 = 0; nucount2 != nucount; ++nucount2)
  168.         {
  169.             ++nuchooser;
  170.         }
  171.     }
  172.  
  173.     //additive monomer reaction
  174.     if(elecs[elecount]->tag == 2)
  175.     {
  176.         //reaction
  177.         nucs[nucount]->next = &*(elecs[elecount]);
  178.         elecs[elecount]->previous = &*(nucs[nucount]);
  179.  
  180.         //reset the molecular weight, number of nodes, number of original chains
  181.         elecs[elecount]->molweight = elecs[elecount]->molweight + nucs[nucount]->molweight;
  182.         elecs[elecount]->nodes = elecs[elecount]->nodes + nucs[nucount]->nodes;
  183.         elecs[elecount]->originalchains = elecs[elecount]->originalchains + nucs[nucount]->originalchains;
  184.  
  185.         //vector shuffling
  186.         nuchooser = nucs.erase(nuchooser);
  187.         nuchooser = nucs.insert(nuchooser, elecs[elecount]);
  188.         elechooser = elecs.erase(elechooser);
  189.     }
  190.  
  191.     //branching monomer reaction
  192.     else if(elecs[elecount]->tag == 3)
  193.     {    
  194.         //fast site
  195.         if(elecs[elecount]->nextbranchtag == 2)
  196.         {
  197.             bool loopy2 = 0;
  198.             looper(nucs[nucount], loopy2, elecs[elecount]->namenumber);
  199.  
  200.             if(loopy2==0)
  201.             {            
  202.             //set the monomers fast site as reacted
  203.             elecs[elecount]->branchfull = 1;
  204.  
  205.             //reaction
  206.             nucs[nucount]->next = &*(elecs[elecount]);
  207.             elecs[elecount]->branch = &*(nucs[nucount]);
  208.  
  209.             //reset the molecular weight, number of nodes, number of original chains
  210.             elecs[elecount]->molweight = elecs[elecount]->molweight + nucs[nucount]->molweight;
  211.             elecs[elecount]->nodes = elecs[elecount]->nodes + nucs[nucount]->nodes;
  212.             elecs[elecount]->originalchains = elecs[elecount]->originalchains + nucs[nucount]->originalchains;
  213.  
  214.             //vector shuffling
  215.             nuchooser = nucs.erase(nuchooser);
  216.             //if electrophile has no electrophilic sites left
  217.             if(elecs[elecount]->nextfull)
  218.                 elechooser = elecs.erase(elechooser);
  219.             }
  220.         }
  221.  
  222.         //slow site
  223.         else if(elecs[elecount]->nextbranchtag == 1)
  224.         {    
  225.             //set the monomers slowsite as reacted
  226.             elecs[elecount]->nextfull = 1;
  227.  
  228.             //reaction
  229.             elecs[elecount]->previous = &*(nucs[nucount]);
  230.             nucs[nucount]->next = &*(elecs[elecount]);
  231.  
  232.             //reset the molecular weight, number of nodes, number of original chains
  233.             elecs[elecount]->molweight = elecs[elecount]->molweight + nucs[nucount]->molweight;
  234.             elecs[elecount]->nodes = elecs[elecount]->nodes + nucs[nucount]->nodes;
  235.             elecs[elecount]->originalchains = elecs[elecount]->originalchains + nucs[nucount]->originalchains;
  236.  
  237.             //vector shuffling
  238.             nuchooser = nucs.erase(nuchooser);
  239.             nuchooser = nucs.insert(nuchooser, elecs[elecount]);
  240.             //electrophile has no electrophilic sites left
  241.             if(elecs[elecount]->branchfull)
  242.                 elechooser = elecs.erase(elechooser);
  243.  
  244.         }
  245.     }
  246.     }
  247. }
  248.  
  249. //the basic run algorithm is as follows
  250. while(nucleophiles.size()!=0)
  251.     {
  252.     //reinitialize counters
  253.     nucounter = 0;
  254.     elecounter = 0;
  255.  
  256.     if(!electrophiles.empty())//if the elecrophiles queue is not empty there are still more monomers to react
  257.     {    
  258.     determine_electrophile(electrophiles, elecounter, addprob, branchprob1, branchprob2, alpha);
  259.     determine_nucleophile(nucleophiles, nucounter, alpha, iter);
  260.  
  261.     addmonomer(nucleophiles, electrophiles, nucounter, elecounter, addprob, branchprob1, branchprob2, alpha, iter);
  262.     }
  263.     else//if the electrophiles queue is empty we add more monomers to react
  264.     {
  265.     initializeaddmons(electrophiles, addnum, namenumber);
  266.     initializebranchmons(electrophiles, branchnum, namenumber);
  267.     }
  268.     iter = iter+1;
  269.     }
  270.  
Apr 6 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.