Thread: terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_all

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    6

    terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_all

    Hi everyone !!

    i have an interesting problem...hope you people can help.
    I am coding in C.
    the code is posted below:
    here is how I define my structure....
    Code: ( text )
    Code:
       1.
          struct IDnTime
       2.
              {   
       3.
                  vector<char *> s;
       4.
                  vector<char *> g;      
       5.
                  struct tm theTime;
       6.
                  struct tm *ptr;   
       7.
                  time_t t;
       8.
              } IDnTimeArray[50];
    Next I inititalize few char variables and then point them with the pointers like thisthis I did to avoid pushing characters themselves in the vectors....now this should allow me to push the addresses of the characters or the poiters to thode characters in the vector)
    Code: ( text )
    Code:
       1.
          char Walk ='w',Bike ='b',Car ='c',UrbanPuT ='u',Rail ='r',Notdecided ='n',Activity ='a';
       2.
              char *walk =&Walk, *bike =&Bike, *car =&Car, *urbanPuT =&UrbanPuT, *rail =&Rail, *notdecided =&Notdecided, *activity =&Activity;
    Then i try to push back like this
    Code: ( text )
    Code:
       1.
          IDnTimeArray[counter1].s.push_back(&Activity);
       2.
          IDnTimeArray[counter1].s.push_back(&Walk);
    and so on.......76,000 character addresses in the vector 's'..and similarly for the vector g as well
    Code: ( text )
    Code:
       1.
          IDnTimeArray[counter1].g.push_back(&Activity);
       2.
          IDnTimeArray[counter1].g.push_back(&Walk);
    the program compiles fine...gr8..then I run it ..it runs for 2 or three seconds ...gr8...then as the pushing back operation in the 's' vector of the array of the structures is completed and the g vector is started it stops .....when I run it on my computer the process it terminated here is
    Code:
    terminate called after throwing an instance of 'std::bad_alloc'
    what(): St9bad_all
    Aborted
    i dont get this .....PLEASE HELPi dont get this .....PLEASE HELP

    Also few may ask whether I try to fill all the 's' and 'g' vectors of the 50 structure array locations together.......
    the answer is NO!...first I approach each structure array element and then i try to fill the s and g vectors of that location(whch is not allowed and the message displayed i mentioned above)...then as i finsh my desired function with these vectors I clear these vectors by
    Code: ( text )
    Code:
       1.
          IDnTimeArray[counter1].s.clear();
       2.
          IDnTimeArray[counter1].g.clear();
    So the memory is freed......

    HELP HELP

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you are coding in C, how did a C++ container like vector<char*> manage to appear in your code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    Hi!!
    the code is in C++ but as I am more familiar with C I am using all its features...it should not cause the probelm I think...it compiles well and shows no error

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It's hard to say from the amount of code posted. See if you can determine what line in the program causes the abort. Maybe try printing the contents of the vectors (if it's too big to print to the screen, print to a file).

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Please don't abuse the "report post" feature just to draw attention to your post.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    Quote Originally Posted by swoopy View Post
    It's hard to say from the amount of code posted. See if you can determine what line in the program causes the abort. Maybe try printing the contents of the vectors (if it's too big to print to the screen, print to a file).

    No no I am damn sure that the vectors take not more than 76000 characters...that's it....
    actualy when i try to push back individually in them (be commenting out the pushing back operation in the other one) it works fine..but when itry to puch them one after the other(without any comenting out ) it shows bad_alloc

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >it shows bad_alloc
    And what's the value of counter1 at the time it shows bad_alloc?

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Not to play moderator here, but might I suggest a move to the C++ forums due to the potential of better responses?

    OK, ok..... I just really want it moved because it irritates me to see "std::badalloc" in the C section of the forums.

    As to the problem itself, I have a feeling we're not seeing the actual code that is causing the issue.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    Quote Originally Posted by MacGyver View Post
    Not to play moderator here, but might I suggest a move to the C++ forums due to the potential of better responses?

    OK, ok..... I just really want it moved because it irritates me to see "std::badalloc" in the C section of the forums.

    As to the problem itself, I have a feeling we're not seeing the actual code that is causing the issue.
    hey thanks for pointing out ...i will start a new thread in C++......I have given the code here and there as well
    if you see the initial value of the counter1 is 10.

    Code:
    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    #include <vector>
    #include "unisam.hpp"
    #include <iostream>
    #include<fstream>
    #define MAX_LENGTH_of_personID 50
    #define MAX_No_of_personID 100
    using namespace std;
    
    int main(int argc, char *argv[])
    {	
    	struct IDnTime 
    	{	
    		char ID[MAX_LENGTH_of_personID];
    		vector<char *> s;
    		vector<char *> g;
    		int dist;
    		char previousEdate[50];
    		char previousEtime[50];
    		struct tm theTime;
    		struct tm *ptr;			
    		time_t t;
    	} IDnTimeArray[MAX_No_of_personID];	
    	char Walk ='w',Bike ='b',Car ='c',UrbanPuT ='u',Rail ='r',Notdecided ='n',Activity ='a';
    	char *walk =&Walk, *bike =&Bike, *car =&Car, *urbanPuT =&UrbanPuT, *rail =&Rail, *notdecided =&Notdecided, *activity =&Activity;
    	struct tm theTime1, theTime2, theTime3, theTime4, theTime5;
    	struct tm *ptr1=&theTime1;				
    	struct tm *ptr2=&theTime2;				
    	struct tm *ptr3=&theTime3;
    	struct tm *ptr4=&theTime4;
    	struct tm *ptr5=&theTime5;
    	double t1, t2, t3,t4, t5,t6;
    	time_t timediff1, timediff2 , timediff3, timediff4;
    	int remainder3, remainder4,pushBackCounter3, pushBackCounter4,remainder2, remainder1,pushBackCounter2, pushBackCounter1, z;
    	FILE *f, *f1, *f3,*f4;   	
    	char c,d, e;
    	int y1=0,y=0,gg=0,hh=0,ii=0,jj=0,kk=0,ggg=0,hhh=0,iii=0,jjj=0,kkk=0,ntab=0,intab=0, ntab1=0, counter=0,flag1,flag_1,counter_1=0, flag2, flag3, match, match1, newlineCounter=0,newlineCounter1=0, counter1=0;	
    	char sDate[50], sTime[50], eDate[50], eTime[50], mode[50], personID[50], personID1[50], ipreviousEdate[50], ipreviousEtime[50];
    	char isDate[50], isTime[50], ieDate[50], ieTime[50], imode[50], ipersonID[50];
    	char IDStartTime[MAX_No_of_personID];
    	char interview_data_file_header[50]="FlammOriginalStages_", interview_data_file[50];	
    	f=fopen("StageStatistics.txt", "r");
    	f3=fopen("Unisam_allDist.txt", "a");
    	int totalDist=0;
    	while ((e=getc(f))!=EOF)
    	{
    		if (e=='\n')
    		{	
    			ntab1=0;
    			y1=0;
    			if (strcmp(personID1,"PersonID")==0)
    				continue;
    			else
    			{
    				match1=0;
    				for (flag_1=counter_1;flag_1>=0;flag_1--)
    				{
    					if (strcmp(IDnTimeArray[flag_1].ID,personID1)==0)
    					match1 =1;					
    				}
    				if (match1==0)
    				{
    					strcpy(IDnTimeArray[counter_1].ID,personID1);
    					IDnTimeArray[counter_1].s.clear();
    					IDnTimeArray[counter_1].g.clear();
    					IDnTimeArray[counter_1].dist=0;
    					IDnTimeArray[counter_1].ptr=&IDnTimeArray[counter_1].theTime;
    					++counter_1;//printf("\n&#37;d\n",counter_1);
    				}
    			}
    		}
    		else if (e=='\t')
    		{	
    			++ntab1;
    			personID1[y1]='\0';
    		}
    		else 
    		{
    			if (ntab1==0)
    			{
    				personID1[y1]=e;
    				++y1;
    			}
    		}
    	}
    	fclose(f);	
    //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    	for (counter1=counter_1-1;counter1>=0;counter1--)
    	{	
    		f=fopen("StageStatistics.txt", "r");
    		while ((c=getc(f))!=EOF)
    		{
    			//printf("%c",c);
    		
    			if (c=='\n')
    			{
    				ntab=0;
    				y=0;
    				gg=0;
    				hh=0;
    				ii=0;
    				jj=0;
    				kk=0;
    				if (strcmp(personID,"PersonID")==0)
    				continue;
    				else
    				{	
    
    					if (strcmp(IDnTimeArray[counter1].ID,personID)==0)
    					{
    						++newlineCounter1;
    						if (newlineCounter1==1)				
    						{
    							strcpy(IDnTimeArray[counter1].previousEdate,eDate);
    							strcpy(IDnTimeArray[counter1].previousEtime,eTime);
    						}
    						strptime(IDnTimeArray[counter1].previousEdate, "%d.%m.%Y", IDnTimeArray[counter1].ptr);
    						strptime(IDnTimeArray[counter1].previousEtime, "%H:%M:%S", IDnTimeArray[counter1].ptr);
    						IDnTimeArray[counter1].ptr->tm_isdst=-1;
    						IDnTimeArray[counter1].t=mktime(IDnTimeArray[counter1].ptr);
    						strptime(sDate, "%d.%m.%Y", ptr1);
    						strptime(sTime, "%H:%M:%S", ptr1);
    						ptr1->tm_isdst=-1;
    						t1=mktime(ptr1);
    						strptime(eDate, "%d.%m.%Y", ptr2);
    						strptime(eTime, "%H:%M:%S", ptr2);
    						ptr2->tm_isdst=-1;
    						t2=mktime(ptr2);
    						if(difftime((time_t)t1, (time_t)IDnTimeArray[counter1].t)>0)
    						{
    							timediff1=difftime((time_t)t1,(time_t)IDnTimeArray[counter1].t);
    							remainder1=timediff1%60;
    							pushBackCounter1=(timediff1-remainder1)/60;//printf("\n%d\n",pushBackCounter1);
    							for (z=0;z<pushBackCounter1;z++)
    							{
    								IDnTimeArray[counter1].s.push_back(&Activity);
    									//printf("a");
    							}
    						}							
    						timediff2=difftime((time_t)t2,(time_t)t1);
    						remainder2=timediff2%60;
    						pushBackCounter2=(timediff2-remainder2)/60;
    						for (z=0;z<pushBackCounter2;z++)
    						{
    							if (strcmp(mode,"walk")==0)
    							{IDnTimeArray[counter1].s.push_back(&Walk);}//printf("w");}
    							if (strcmp(mode,"bike")==0)
    							{IDnTimeArray[counter1].s.push_back(&Bike);}//printf("b");}
    							if (strcmp(mode,"car")==0)
    							{IDnTimeArray[counter1].s.push_back(&Car);}//printf("c");}
    							if (strcmp(mode,"urbanPuT")==0)
    							{IDnTimeArray[counter1].s.push_back(&UrbanPuT);}//printf("u");}
    							if (strcmp(mode,"rail")==0)
    							{IDnTimeArray[counter1].s.push_back(&Rail);}//printf("r");}
    							if (strcmp(mode,"notdecided")==0)
    							{IDnTimeArray[counter1].s.push_back(&Notdecided);}//printf("n");puts(personID);}
    						}							
    						strcpy(IDnTimeArray[counter1].previousEdate,eDate);
    						strcpy(IDnTimeArray[counter1].previousEtime,eTime);
    					}
    				}
    			}			
    			else if (c=='\t')
    			{
    				++ntab;	
    				personID[y]='\0';
    				sDate[gg]='\0';
    				sTime[hh]='\0';
    				eDate[ii]='\0';
    				eTime[jj]='\0';
    				mode[kk]='\0';			
    			}
    			else 
    			{
    				if (ntab==0)
    				{	
    					personID[y]=c;
    					++y;
    				}
    				if (ntab==8)
    				{
    					sDate[gg]=c;
    					++gg;
    				}
    				if (ntab==9)
    				{
    					sTime[hh]=c;
    					++hh;
    				}
    				if (ntab==14)			
    				{
    					eDate[ii]=c;++ii;
    				}
    				if (ntab==15)			
    				{
    					eTime[jj]=c;++jj;
    				}
    				if (ntab==21)			
    				{
    					mode[kk]=c;++kk;
    				}		
    			}
    		}
    		fclose(f);
    		newlineCounter1=0;
    		strcpy(interview_data_file, interview_data_file_header);
    		strcat(interview_data_file, IDnTimeArray[counter1].ID);		
    		strcat(interview_data_file,".txt");
    		puts(interview_data_file);
    		f1=fopen(("%s",interview_data_file),"r");
    		newlineCounter=0;
    		strcpy(ipreviousEdate, "a");
    		strcpy(ipreviousEtime, "a");	
    		while ((d=getc(f1))!=EOF)
    		{	
    			if (d=='\n')
    			{	++newlineCounter;
    				intab=0;
    				
    				ggg=0;
    				hhh=0;
    				iii=0;
    				jjj=0;
    				kkk=0;
    				if (strcmp(isDate,"StartingDate")==0)
    				continue;
    				else
    				{				
    						if (newlineCounter==2)				
    						{
    							strcpy(ipreviousEdate, ieDate);
    							strcpy(ipreviousEtime, ieTime);//puts(ipreviousEtime);
    						}
    						
    						strptime(isDate, "%d.%m.%Y", ptr3);//puts(isDate);
    						strptime(isTime, "%H:%M:%S", ptr3);//puts(isTime);
    						ptr3->tm_isdst=-1;
    						t4=mktime(ptr3);//printf("%d\n",t4);
    		
    
    						strptime(ieDate, "%d.%m.%Y", ptr4);//puts(ieDate);
    						strptime(ieTime, "%H:%M:%S", ptr4);//puts(ieTime);
    						ptr4->tm_isdst=-1;
    						t5=mktime(ptr4);//printf("%d\n",t5);
    
    						strptime(ipreviousEdate, "%d.%m.%Y", ptr5);//puts(ipreviousEdate);
    						strptime(ipreviousEtime, "%H:%M:%S", ptr5);//puts(ipreviousEtime);
    						ptr5->tm_isdst=-1;
    						t6=mktime(ptr5);//printf("%d\n",t4);
    						if(difftime((time_t)t4, (time_t)t6)>0)
    						{
    							timediff3=difftime((time_t)t4,(time_t)t6);
    							remainder3=timediff3%60;
    							pushBackCounter3=(timediff3-remainder3)/60;
    							for (z=0;z<pushBackCounter3;z++)
    							{
    								IDnTimeArray[counter1].g.push_back(&Activity);//printf("a");
    							}
    						}
    						//printf("@@@");
    						timediff4=difftime((time_t)t5,(time_t)t4);
    						remainder4=timediff4%60;
    						pushBackCounter4=(timediff4-remainder4)/60;
    
    						for (z=0;z<pushBackCounter4;z++)
    						{
    							if (strcmp(imode,"walk")==0)
    							{IDnTimeArray[counter1].g.push_back(&Walk);}//printf("w");}
    							if (strcmp(imode,"bike")==0)
    							{IDnTimeArray[counter1].g.push_back(&Bike);}//printf("b");}
    							if (strcmp(imode,"car")==0)
    							{IDnTimeArray[counter1].g.push_back(&Car);}//printf("c");}
    							if (strcmp(imode,"urbanPuT")==0)
    							{IDnTimeArray[counter1].g.push_back(&UrbanPuT);}//printf("u");}
    							if (strcmp(imode,"rail")==0)
    							{IDnTimeArray[counter1].g.push_back(&Rail);}//printf("r");}
    							
    						}
    						
    						strcpy(ipreviousEdate, ieDate);
    						strcpy(ipreviousEtime, ieTime);
    																
    
    				}				
    			}
    			else if (d=='\t')
    			{
    				++intab;
    
    	
    				isDate[ggg]='\0';
    				isTime[hhh]='\0';
    				ieDate[iii]='\0';
    				ieTime[jjj]='\0';
    				imode[kkk]='\0';
    				
    				
    				
    			}
    			else 
    			{
    	
    				if (intab==4)
    				{
    					isDate[ggg]=d;
    					++ggg;
    				}
    				if (intab==5)
    				{
    					isTime[hhh]=d;
    					++hhh;
    				}
    				if (intab==9)			
    				{
    					ieDate[iii]=d;++iii;
    				}
    				if (intab==10)			
    				{
    					ieTime[jjj]=d;++jjj;
    				}
    				if (intab==11)			
    				{
    					imode[kkk]=d;++kkk;
    				}						
    			}
    		}
    
    		UniSAM mySAM;
    
     		IDnTimeArray[counter1].dist = mySAM.getDistance(IDnTimeArray[counter1].s, IDnTimeArray[counter1].g);
    
    		totalDist=totalDist+IDnTimeArray[counter1].dist;
    
     		cerr << "Distance: " << IDnTimeArray[counter1].dist << endl;
    //  		cerr << endl;
    
      		IDnTimeArray[counter1].dist = mySAM.getDistance(IDnTimeArray[counter1].g, IDnTimeArray[counter1].s);
    
    //		cerr << "Distance reversed: " << IDnTimeArray[counter1].dist << endl;
    		IDnTimeArray[counter1].s.clear();
    		IDnTimeArray[counter1].g.clear();
    
    		
    	}		
    fprintf(f3,"%d\n",totalDist);
    printf("%d\n",totalDist);
    
      return 0;
    }

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    Quote Originally Posted by swoopy View Post
    >it shows bad_alloc
    And what's the value of counter1 at the time it shows bad_alloc?
    the counter1 starts with the value 10....actually counter1 is just to access the structure array location. initial while loop creates all the structure locations and then they are approached in the reverse order for the pushing back and other operations

    thanks!

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    Quote Originally Posted by MacGyver View Post
    Not to play moderator here, but might I suggest a move to the C++ forums due to the potential of better responses?

    OK, ok..... I just really want it moved because it irritates me to see "std::badalloc" in the C section of the forums.

    As to the problem itself, I have a feeling we're not seeing the actual code that is causing the issue.
    how can i move this thread to C++ forum...is there a way or I have to just create a new one over there?

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by du_409 View Post
    how can i move this thread to C++ forum...is there a way or I have to just create a new one over there?
    Wait for a moderator to move it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions which return a reference
    By Stonehambey in forum C++ Programming
    Replies: 10
    Last Post: 07-05-2008, 01:43 PM
  2. Which one is "quicker" ?
    By AloneInTheDark in forum C# Programming
    Replies: 2
    Last Post: 02-08-2008, 09:23 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. Class function that returns the instance it's called from
    By L Boksha in forum C++ Programming
    Replies: 4
    Last Post: 05-25-2002, 11:52 AM