473,412 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

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

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
0 2586

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

Similar topics

12
by: Fred Ma | last post by:
Hello, I was looking at Meyers's "Effective STL", item 23 about choosing between vectors and maps (at least that the choice for me). In many cases, using sorted vectors is faster for lookups. ...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
4
by: mp | last post by:
I am doing pairwise comparisons between 2 vectors of chars and permuting one vector and storing the resulting calculations in a vector<float> then I find a p-value among other stats. I have to do...
7
by: Pieter | last post by:
Hi, Something strange is happing with my Windowf Forms application (VB.NET 2005): The users had all local Administrator-rights on there pc. We changed it to the 'normal' users, and now they...
9
by: Jeff | last post by:
Hello- Ive never used a vector or vectors in C++ so I have a question for you all. I know I can dynamically create the size I need upfront, but is it possible to create them on the fly...
13
by: M O J O | last post by:
Often when I run my aspnet 2.0 vb application, I get this exception.... "Microsoft (R) Visual Basic Compiler has encountered a problem and needs to close. We are sorry for the inconvenience." ...
2
by: wuzertheloser | last post by:
Use the program skeleton below (starting with #include <stdio.h>) as the starting point for quiz4. Add the necessary code to the functions prob1() and prob2(), and add the other 2 functions, as...
0
by: John A. Bailo | last post by:
I just got a new server built by my network group with Windows 2003. I updated a c# console app that was running under 2000/.NET 1.1 to .NET 2.0 When running it on the new machine it runs to...
6
by: amscompit | last post by:
I have a written the following code. #include<iomanip> #include<fstream> #include<vector> #include<cctype> using namespace std;
4
by: =?Utf-8?B?Um9iZXJ0IFN0eW1h?= | last post by:
Hello All, I am new to these forums and reasonably new to Visual Basic.net. I have been converting a couple of visual basic 6 programs to VB.NET. The programs began failing under windows Vista, so...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.