GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Jul-2008, 09:15
upadhyad upadhyad is offline
New Member
 
Join Date: Jul 2008
Posts: 5
upadhyad is on a distinguished road

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  
Old 10-Jul-2008, 17:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

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


Quote:
Originally Posted by upadhyad
... 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

Ummm...no it isn't.

The clear() function makes the vector "empty" in the sense that its size is reported as zero and it has no elements that can be accessed, but the compiler is not required to create code that returns memory allocated to that vector to the system. (In my experience none of them do.)

Recent compilers that I have used will allow a Gigabyte or two of dynamic memory allocation before throwing the badalloc exception.

Try the following on your system:

I won't even try to figure out what you are doing or why, but I suggest you try the following on your system.

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;
int main()
{
    struct IDnTime
          {
              vector<char *> s;
              vector<char *> g;
              struct tm theTime;
              struct tm *ptr;
              time_t t;
          } IDnTimeArray[50];


    char a = 'a';
    char b = 'b';
    int counter1 = 0;

    for (unsigned counter1 = 0; counter1 < 50; counter1++) {
        vector<char *>empty;
        for (unsigned j = 0; j < 50000000; j++) {
            IDnTimeArray[counter1].s.push_back(&a);
            IDnTimeArray[counter1].s.push_back(&b);
        }
        cout << "IDnTimeArray[" << setw(2) << counter1
             << "].s.size() = "
             << IDnTimeArray[counter1].s.size() << endl;
        IDnTimeArray[counter1].s.clear();
        //IDnTimeArray[counter1].s.swap(empty);

        cout << setw(29)
             << IDnTimeArray[counter1].s.size() << endl;
    }

    IDnTimeArray[counter1].s.clear();
        cout << left << setw(12) 
             << counter1 << ": IDnTimeArray[" << setw(2) << counter1
             << "].s.size() = "
             << IDnTimeArray[counter1].s.size() << endl;

    return 0;
}

On my LInux system (GNU compiler), here's the output:
Code:
IDnTimeArray[ 0].s.size() = 100000000 0 IDnTimeArray[ 1].s.size() = 100000000 0 IDnTimeArray[ 2].s.size() = 100000000 0 IDnTimeArray[ 3].s.size() = 100000000 0 terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_alloc

Now, comment out the clear() statement and uncomment the swap()

Mine runs to completion. How about yours?

Regards,

Dave

Footnote: I have seen other methods such as using vector::resize(0) recommended on other forums. This particular thing isn't guaranteed to make the internal storage smaller either. (In my experience it doesn't work---try it in my simple program above and tell us whether it works for you.) The swap() scheme always works for me. Maybe there are other ways.
  #3  
Old 10-Jul-2008, 18:06
upadhyad upadhyad is offline
New Member
 
Join Date: Jul 2008
Posts: 5
upadhyad is on a distinguished road

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


On my comp it works exactly same as it did on yours! That was a great advice...but it did not work. because i am having the problem while pushing back in the g vector. I have realized that pushing in s vector goes fine when done individually and pushing in g vector goes fine when done individually but when i try one after the other without commenting out the other one (g after s in my case I case) I get a bad alloc. my program doesn't even reach my s.clear statement...
  #4  
Old 10-Jul-2008, 19:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

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


Quote:
Originally Posted by upadhyad
... because i am having the problem while pushing back in the g vector...my program doesn't even reach my s.clear statement...

On my example (with four-byte pointers on my 32-bit Linux system) it runs out of steam at something more that 3 Gigabytes. Exactly how much memory are you requiring for each step in your program? If you haven't calculated it, maybe it's time to do so.


To hammer home the point, I suggest that you look at something like the following simple test program to see what your limits are.
CPP / C++ / C Code:
#include <iostream>
using namespace std;

char *getsomebytes(int n)
{
    return new char[n];
}

int main()
{
    char *pt;
    int millions = 10;

    cout << "Getting storage in " 
         << millions << " million byte chunks." << endl;

    int total = 0;
    while (1) {

        pt = getsomebytes(millions*1000000);
        total += millions;
        cout << "Total = " << total << " million bytes." << endl;
    }
    return 0;
}#include <iostream>
using namespace std;

char *getsomebytes(int n)
{
    return new char[n];
}
int main()
{
    char *pt;
    int millions = 10;

    cout << "Getting storage in " 
         << millions << " million byte chunks." << endl;

    int total = 0;
    while (1) {

        pt = getsomebytes(10000000);
        total += millions;
        cout << "Total = " << total << " million bytes." << endl;
    }
    return 0;
}

Output on my 32-bit Linux system (GNU gcc Version 4.1.2):
Code:
Getting storage in 10 million byte chunks. Total = 10 million bytes. Total = 20 million bytes. Total = 30 million bytes. Total = 40 million bytes. Total = 50 million bytes. Total = 60 million bytes. Total = 70 million bytes. Total = 80 million bytes. Total = 90 million bytes. . . About 300 more lines like these . Total = 3140 million bytes. Total = 3150 million bytes. Total = 3160 million bytes. Total = 3170 million bytes. Total = 3180 million bytes. terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_alloc

This is kind of a "best case" program. You won't be able to get much more than this amount at any given time, no matter how hard you try.

Remember, that, in general, allocating and deallocating might leave the memory fragmented in such a way that you don't have enough contiguous memory to allocate a new block even though the total amount might be sufficient.

I won't try to understand (or pretend to understand) exactly what you are doing or why you are doing it. I will say that if you find yourself allocating and deallocating large blocks of memory, then I might suggest that maybe the way to go would be to allocate a block once outside the loop and use that same block repeatedly inside your loop. Then, once I start giving advice like that, I would have to try to figure out what the heck you really need to do, and I don't think I will, just now.

Regards,

Dave
Last edited by davekw7x : 10-Jul-2008 at 20:38.
  #5  
Old 10-Jul-2008, 22:58
upadhyad upadhyad is offline
New Member
 
Join Date: Jul 2008
Posts: 5
upadhyad is on a distinguished road

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


It seems the comps are similar...I get the same output as you do !
.there is some other problem
I found an interesting thing that if i push back in g first and comment out pushing back in s it works
if i push back in s first and comment out pushing back in g it works
if I push s after g it doesn't work for g but works for s
if I push g after s it doesn't work for g but works for g

I am getting sick!
  #6  
Old 10-Jul-2008, 23:56
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

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


Quote:
Originally Posted by upadhyad
...there is some other problem..

So: If you don't think that you need 3 GBytes of memory for your application, but you get the bad_alloc exception, I would guess that there is some other problem.

With the fragments of code that you showed, there's not much way that we can guess what it is.

Oh, well...

Regards,

Dave

Footnote: Did you try the program with a smaller amount of data? Did you confirm that it pushes exactly what you think it should for each item?
  #7  
Old 11-Jul-2008, 05:10
upadhyad upadhyad is offline
New Member
 
Join Date: Jul 2008
Posts: 5
upadhyad is on a distinguished road

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


The program runs fine for small amount of data ...and it does exactly what it should...i will try to post the code here..please see the comments for a faster glance....thanks
CPP / C++ / C 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 											//the structure is defined here
	{	
		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];								//the structure array
	char Walk ='w',Bike ='b',Car ='c',UrbanPuT ='u',Rail ='r',Notdecided ='n',Activity ='a';	//the character variables defined here and the pointer to them in the next line
	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;									//these lines cannot cause any problem so scroll down  to the next comment
	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)									//this while loop---initialization of structure array locations
	{												//scroll down to see the loop which does the pushing back operation
		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%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--)					// the pushing back in g and s vectors of each struct array location is handled by this loop
	{		
		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);			//pushing back starts here for s
									//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");}		//pushing back continues for s
							if (strcmp(mode,"bike")==0)
							{IDnTimeArray[counter1].s.push_back(&Bike);}//printf("b");}		//pushing back continues 
							if (strcmp(mode,"car")==0)
							{IDnTimeArray[counter1].s.push_back(&Car);}//printf("c");}		//pushing back continues 
							if (strcmp(mode,"urbanPuT")==0)
							{IDnTimeArray[counter1].s.push_back(&UrbanPuT);}//printf("u");}		//pushing back continues 
							if (strcmp(mode,"rail")==0)
							{IDnTimeArray[counter1].s.push_back(&Rail);}//printf("r");}		//pushing back continues 
							if (strcmp(mode,"notdecided")==0)
							{IDnTimeArray[counter1].s.push_back(&Notdecided);}//printf("n");puts(personID);}	//pushing back continues
						}							
						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");		//pushing back starts here for g	
							}
						}
						//printf("@@@");
						timediff4=difftime((time_t)t5,(time_t)t4);						//######the program gets somewhere here !!!!###
						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");}			//pushing back continues here for g
							if (strcmp(imode,"bike")==0)
							{IDnTimeArray[counter1].g.push_back(&Bike);}//printf("b");}			//pushing back continues
							if (strcmp(imode,"car")==0)
							{IDnTimeArray[counter1].g.push_back(&Car);}//printf("c");}			//pushing back continues
							if (strcmp(imode,"urbanPuT")==0)
							{IDnTimeArray[counter1].g.push_back(&UrbanPuT);}//printf("u");}			//pushing back continues
							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();
		IDnTimeArray[counter1].s.swap(empty);
		IDnTimeArray[counter1].g.swap(empty);

		
	}		
fprintf(f3,"%d\n",totalDist);
printf("%d\n",totalDist);

  return 0;
}
  #8  
Old 11-Jul-2008, 09:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

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


Quote:
Originally Posted by upadhyad
....i will try to post the code here
[code]
CPP / C++ / C Code:
.
.
.
#include "unisam.hpp"
.
.
.

So: Where is unisam.hpp? How about some small example files for the ones that you use? I can't speak for others, but I don't see the point if my trying to guess what your 350+ lines of code is supposed to do. However: See Footnote.

The point that I was trying to make in my previous posts is that you, not i, can debug your program by breaking unknown stuff into little chunks. You have already established that your system supports something like 3 GBytes of allocated memory (the kind of memory that your vectors get). Now figure out whether your program can actually need this much. If you used some test code based on something that someone else gave you, and it worked, then make sure you implement exactly the same functionality into your program. See Footnote (again).

Regards,

Dave

Footnote: Your code uses a vector named "empty." I suppose you intended it to be something like my example. In my example, the vector was declared at the top of the loop. The whole point is that, after the first time through the loop, the declaration causes the destructor to be called before constructing a new vector (therefore de-allocating the huge amount of storage that was swapped into it). If the missing header file has a global declararation for "empty", then it won't work, since the storage is never released. (Try my previous example using the swap function, but put the declaration for empty in front of the loop instead if inside it as I showed.)

On the other hand, if the missing header file doesn't have a declaration for the vector, then your code can't possibly compile, and we might wonder what else is different between the code that you posted and the code that you are compiling.

So: which is it?
Last edited by davekw7x : 11-Jul-2008 at 09:39.
  #9  
Old 12-Jul-2008, 16:42
upadhyad upadhyad is offline
New Member
 
Join Date: Jul 2008
Posts: 5
upadhyad is on a distinguished road

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


Quote:
Originally Posted by davekw7x
So: Where is unisam.hpp? How about some small example files for the ones that you use? I can't speak for others, but I don't see the point if my trying to guess what your 350+ lines of code is supposed to do. However: See Footnote.

The point that I was trying to make in my previous posts is that you, not i, can debug your program by breaking unknown stuff into little chunks. You have already established that your system supports something like 3 GBytes of allocated memory (the kind of memory that your vectors get). Now figure out whether your program can actually need this much. If you used some test code based on something that someone else gave you, and it worked, then make sure you implement exactly the same functionality into your program. See Footnote (again).

Regards,

Dave

Footnote: Your code uses a vector named "empty." I suppose you intended it to be something like my example. In my example, the vector was declared at the top of the loop. The whole point is that, after the first time through the loop, the declaration causes the destructor to be called before constructing a new vector (therefore de-allocating the huge amount of storage that was swapped into it). If the missing header file has a global declararation for "empty", then it won't work, since the storage is never released. (Try my previous example using the swap function, but put the declaration for empty in front of the loop instead if inside it as I showed.)

On the other hand, if the missing header file doesn't have a declaration for the vector, then your code can't possibly compile, and we might wonder what else is different between the code that you posted and the code that you are compiling.

So: which is it?

Thanks dave actually i got the rpoblem now

th problem was in the unisam.cpp..not in the program that i have written about it...i got to know about it only whne i debuggrd my program...
thanks for helping me out!
people like u rock!!!
this froum is great
thanks again
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:27.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.