![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_allHi 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:
Next I inititalize few char variables and then point them with the pointers like this Code: ( text ) Code:
Then i try to push back like this Code: ( text ) Code:
and so on.......76,000 character addresses in the vector 's'..and similarly for the vector g as well Code: ( text ) Code:
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:
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:
So the memory is freed...... HELP HELP |
|||
|
#2
|
|||
|
|||
Re: terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_allQuote:
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:
On my LInux system (GNU compiler), here's the output: Code:
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
|
|||
|
|||
Re: terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_allOn 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
|
|||
|
|||
Re: terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_allQuote:
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:
Output on my 32-bit Linux system (GNU gcc Version 4.1.2): Code:
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
|
|||
|
|||
Re: terminate called after throwing an instance of 'std::bad_alloc' what(): St9badIt 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
|
|||
|
|||
Re: terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_allQuote:
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
|
|||
|
|||
Re: terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_allThe 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:
|
|
#8
|
|||
|
|||
Re: terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_allQuote:
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
|
|||
|
|||
Re: terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_allQuote:
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 GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The