std::bad_alloc problem

Hi,
I am new to C++ and have got std::bad_alloc error when I run the code. I tried going through the forum on similar issues but I couldnt correct my error. The code is as follows: Please help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <fstream> 
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <time.h>
#include <math.h>
#define MAX_LINE_LENGTH 6000
using namespace std;
class node
{
public:                          
	int gate_num;
    string  gate_type;
    int weight;
	string  gate_input[200];
	string  gate_output;
};
class net
{
public:            
	int net_num;string  net_gates[5000];
};
int main ()
{
    vector<node> node_list; 
    node obj;
    char * tok; 
    int i=-1;
    vector<string> parta;
    vector<string> partb;
    time_t start,end;
    start = time(NULL);
    int partanum,partbnum=0;
    int total_gate_count=0 ;
    int no_of_inp,no_of_out =0;
    int len,j,token_num1;
    char * inp_tok[MAX_LINE_LENGTH];
    ofstream result;
    result.open("result1.txt");
    char  input_name[MAX_LINE_LENGTH];
    ifstream myfile;
   myfile.open("iofile6.txt");
   char line[MAX_LINE_LENGTH];
   int Line_num = 0;
   while(!myfile.eof())
   {
     myfile.getline(line,MAX_LINE_LENGTH);
     Line_num++;
    
              if( line[0] == '\0' )
              {
              }
         else {
         if ( (strncmp(line,"INPUT",5) ==0 ))
             {
          total_gate_count++;                  
          no_of_inp++;
           node obj;
             i++;
                         
        obj.gate_type = "INPUT";
          obj.gate_num = i;
         tok = strtok (line,"()");
         if ( tok != NULL ) 
           {  
            token_num1=1;
              while (tok != NULL)
              {
               if(token_num1==1)
                {
            obj.gate_type = tok; token_num1++;
             }
else
        {   obj.gate_output=tok;
                   if(i%2==0)
                     {
                parta.push_back(obj.gate_output);
                       }   
                     else
                          {
                    partb.push_back(obj.gate_output);
					}    
                                       }
                       tok = strtok (NULL,"()");
                    }	
                    node_list.push_back(obj);  
                    }	
                    }
                     else if ( (strncmp(line,"OUTPUT",6) ==0 ))
                    {
                     }
        else if 
        (line[0]== '#')
	{
	// ignore it
	}
        else { 
             total_gate_count++;
               tok = strtok (line," =,()");
                   node obj;
                   
                   int token_num = 1;
                   while (tok != NULL)
                         {
                          switch(token_num)
                            {
                        case 1: 
                            obj.gate_output = tok;
                           token_num++;
                           break;
                          case 2:
i++;
if(i%2==0)
      {
     parta.push_back(obj.gate_output);
   }   
   else
   {
         partb.push_back(obj.gate_output);
      }  
  obj.gate_type = tok;
   obj.gate_num=i;
   token_num++;
 break;
 default : 
  obj.gate_input[token_num-3] = tok;
  token_num++;
  break;
                  }     
           tok = strtok (NULL," =,()");
                 }
             node_list.push_back(obj); 
            }
           }          
      }  

      vector<net> net_list;
      int con_num ;
      int itr,p;
      int cost_itr,sa,sb;
      double r1,r;
      double boltz;
      int disp;
      int t1,t2;
      int temp;
      int cost,cost1;
      cost1=0;
      string node_to_be[2];
      int partaweight=0,partbweight=0,partaweight1=0,partbweight1=0;
      int itr_loop;
      int inp_count6;
      int flag;
      int num_of_moves;
      int inp_count;
      int inp_count1;
      float cs = 0.80;
      float cool = 3.0;
      int flag2;
vector<string>::iterator var;
      float flag3;
      float i1 = i;
      float move=5.0;
      float cool1=cool;
for ( cool;cool>0.0005;cool=(cool*cs))
{  
flag3=0;
for ( num_of_moves=0;num_of_moves<move;num_of_moves++)
{   
    flag2=0;
     partaweight=0;partbweight=0;
    if(cool<cool1)
    {
    if(partaweight1> (1.5*partbweight1))
    {
   disp =  rand() % (parta.size());
   node_to_be[0] = parta[disp];
   parta.erase(parta.begin()+disp);
   partb.push_back(node_to_be[0]);

   flag =0;
   }
   else
   {
   disp =  rand() % (partb.size());
   node_to_be[1] = partb[disp];
   partb.erase(partb.begin()+disp);
   parta.push_back(node_to_be[1]);
   flag =1;
   }
   }
for(itr =0 ;itr <=i ; itr++ )
   {
   net obj1;
   p=1;
   obj1.net_num = itr+1;
   obj1.net_gates[0] = node_list[itr].gate_output;
   inp_count6=0  ;
   if( node_list[itr].gate_type == "INPUT")
   {inp_count6=1;}
   else
   {
   while( node_list[itr].gate_input[inp_count6] != "\0" )
    {
    inp_count6++;
     }
    }
    if( inp_count6 > 0)
   {node_list[itr].weight = inp_count6;}
   else {node_list[itr].weight = 0;}
         for ( itr_loop =0; itr_loop <=i ; itr_loop++ )
         {  
              while( node_list[itr_loop].gate_input[inp_count] != "\0" )
                    {
                     inp_count++;
                    }
                    inp_count1=0;
                    if ( inp_count!=0)
                    {
                    for(inp_count1;inp_count1<=inp_count-1;inp_count1++)
  {            
  if(node_list[itr].gate_output == node_list[itr_loop].gate_input[inp_count1])
                             {
                              obj1.net_gates[p] = node_list[itr_loop].gate_output; p++;
                             }
                            }
                    } 
                } 
                net_list.push_back(obj1);
 var =find(parta.begin(),parta.end(),node_list[itr].gate_output);
                if(var != parta.end())
                  {
partaweight = partaweight+node_list[itr].weight;
   }   
    else
   {
 partbweight = partbweight+node_list[itr].weight;
                                        }   
 } 
          cost_itr=0;sa=0;sb =0;
          cost=0;
          temp =0;
          //partanum=partbnum=0;
          for (cost_itr=0;cost_itr<=i;cost_itr++)
          {
               sa,sb=0;
               sa=0;sb=0; 
                temp=0;                             
              while( net_list[cost_itr].net_gates[temp] != "\0" )
                {
               temp++;
           }       
                  
            while(temp >=0 ) 
            {    
               t1=parta.size();
               t2=partb.size(); 
            
                while(t1>0)
                    {   
                     if(net_list[cost_itr].net_gates[temp]==parta[t1-1])  
            { 
        sa++;
           }
       t1--; 
        }
    while(t2>0)
      {    
    if(net_list[cost_itr].net_gates[temp]==partb[t2-1])
    {
    sb++;
    }
   t2--; 
   }
            temp--;
            }
            if((sa&&sb)!=0)
                   {cost++;}
           }
         if( cost-cost1 > 0 && cool <cool1)
          {
          
          boltz=exp(-(cost-cost1)/cool);
          r = rand();
          r1 = 1/r;

          if ( r1 > boltz && flag ==0 )
          { result << "WAPAS "<<endl;
               flag3++;    
               flag2=1;    
               parta.push_back(node_to_be[0]);
               partb.erase(partb.begin()+partb.size()-1);
          }
          if ( r1 > boltz && flag ==1 )
          {   result << "WAPAS"<<endl;
              flag2=1;
              flag3++;
               partb.push_back(node_to_be[1]);
               parta.erase(parta.begin()+parta.size()-1);
          }
          }
          if(flag2 !=1)
         {cost1= cost;}
         if( flag2 !=1 )
    {partaweight1=partaweight; partbweight1 = partbweight;}
       
         }
         if((1-(flag3/move)) <=0.6)
         {  cs = 0.99;}
}

}


For small number of iterations the code is working fine, but with large input file and large number of iterations it is giving an error.
Last edited on
Read this:
http://cplusplus.com/forum/articles/16853/


std::bad_alloc is thrown when a memory allocation fails, which is actually kind of hard to do. You'd have to either have a really really lame computer, or your program must be using TONS of memory. Or you're allocating way too much contiguous memory at once.

If you could point out the line of code where you get the bad_alloc exception, that would be helpful.
Thanks for the suggestion. The error is coming in a loop which starts at line 166. After some 100 iteration, it is giving this error. Dont know the exact place where the error is coming as it is in a loop after many iterations.
Please suggest any debugging method.
Error is coming at this line all the time
net_list.push_back(obj1);
class net_list is defined at the beginning of the code.
Any idea what is going wrong ?
I got it resolved. I was putting so many things inside the loop which i needed to evaluate only once. i put them outside and is working fine now.
Topic archived. No new replies allowed.