473,659 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

14 New Member
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 2604

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

Similar topics

12
1651
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. The two reasons are (1) that vector elements are smaller, since map elements need at least 3 pointers, and (2) contiguity of vector elements in memory ensure fewer page faults.
3
3238
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 there anyway where I can accomodate both the vector types into a single set. Something like a set<vector<void*>, my_compare func >. Right now, I am having them as two different set dayatypes.
4
2152
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 1,000,000 permuations for each pair, with a total of 2000 pairs to compare. Is a vector the appropriate container for such a job? Or is this a newbie mistake? Currently I am using a vector of pointers to an object which contains my pair of...
7
1633
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 can't run our V.NET application anymore! The application starts (you can see the screen and caption etc), and than they get the well known error "MyApplication has encountered a problem and needs to close" with the "send error report"-button etc.
9
15898
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 dynamically? That is, for a 2 dim array, I want to create the first element and then push_back the columns after that:
13
1635
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." Error signature: AppName: devenv.exe AppVer: 8.0.50727.42 AppStamp:4333e699 ModName: ntdll.dll ModVer: 5.1.2600.2180 ModStamp:412e8603 fDebug: 0 Offset: 00018fea
2
8683
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 described in the text below. You do not need to change anything in main(). In void prob1(void), take a double floating-point number x from the keyboard and compute the function f(x), which is defined by:
0
1228
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 completion (based on trace messages) and at the end of Main() the OS puts up a dialog that read ".exe has encountered a problem and needs to close. We are sorry for the inconvenience."
6
2325
by: amscompit | last post by:
I have a written the following code. #include<iomanip> #include<fstream> #include<vector> #include<cctype> using namespace std;
4
2540
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 I used the facilities of Visual Studio 2005 to convert them to .NET and then cleaned up the errors. The applications now use both Flexgrid and the printing compatablity library. The applications run great under Vista (where they were...
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8539
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.