![]() |
|
#1
|
|||
|
|||
2D Vector Memory ProblemsI have been having a serious problem relating to memory allocation and arrays/vectors in C++ using the G++ compilier on both a windows machine(Dev-C++) and linux(debian). I would post all the code, but it is proprietary technology(I'm a Graduate Student) and therefore I can't share it openly without permission.
That said, the part of the code that is having a problem is not the proprietary part, so I will give a redacted version of what I am doing and explain the error I am getting. Basically, the program has 2 classes, which we will Trainer and Runner. The main function merely calls an instance of Trainer once with certain parameters, which in turn calls one instance of Runner with certain parameters. The redacted header files for each look something like this: CPP / C++ / C Code:
CPP / C++ / C Code:
Basically, each class has a 2D Vector which holds training set data and paramter data respectively. These do not change in size once filled, and the training data never changes once loaded. Here is the constructor and load data for the Runner class: CPP / C++ / C Code:
This is the constructor and load data for the Trainer class: CPP / C++ / C Code:
The Trainer then goes through iterations of setting certain paramters in the Runner and running it, then mutating the parameters based on the output from that process. Unfortunately, regardless of if I try to load the data from a file or randomly generate it, it will often fire off bad allocation errors or crash the program altogether. The same thing happens if I use float ** and "new" to create the 2D Vectors. Any ideas? Also: 1. With float ** used instead of vectors, it basically doesn't work and crashes. With the vectors, I can at the very least get it to skip past the ones it allocates badly so that it will run for a few generations(sometimes), though loading data from the file for sig_data is very hit or miss. 2. It uses only floats, though sometimes when it makes a bad allocation error, it fills a spot with junk data. Basically, the program uses the training data values, along with the sigma values, in a complex series of mathematical equations. The vectors store floats and sig_data only stores positive floats. The program is using Differential Evolution at one point, so mutcross_sig is filled with parameters based on mutated versions of the one it is competing against. In other words, for each sig_data[i], mutcross becomes something like: mutcross[j] = sig_data[i][j] * 1.2; Anyways, the try/catch actually catches a few bad_allocs, but I don't know why I am getting them or how to handle them. Also, if I try to add ifstreams or ofstreams, it causes a lot of errors too. Backtrace in GDB is no help. 3. When it crashes tends to change wildly if I make any kind of adjustment to the program. Even if I add a cout it might change which function it happens in. Before I had the try/catch, it would sometimes fail when reading the data into the file, or when passing the vector back from load_training_data. Sometimes it would crash anytime I tried to cout the contents of the vector/array or tried to use them in a calculation. Sometimes it runs fine for 20+ generations through the mutation and selection process, then crashes. 4. It might also be worth noting that if I don't load the sig_data from a file, and I keep the population size small, it almost never crashes. Unfortunately I need both of those features working. Last edited by admin : 12-Feb-2009 at 20:59.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|||
|
#2
|
||||||||
|
||||||||
Re: 2D Vector Memory ProblemsQuote:
So, here's a suggestion: Boil things down to something that you can post that can actually be compiled and will show the problem. There are several advantages to that: 1. If someone else can reproduce the problem, maybe you can get some help. 2. In the process of creating an executable subset you may actually spot something in your code that is causing the problem. 3. If the subset doesn't show the problem, then you may be able to add stuff incrementally to get to the point if misbehaviour. Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
The idea isn't to get something useful, but to see if adjusting the sizes gives reasonable values for total memory required. Code:
With a program like you described (not a lot of de-allocation and re-allocation) array and vector sizes containing a couple of GigaBytes should be possible with any reasonable compiler and operating system. A final note: Sometimes allocation fails not because the program actually asks for too much memory, but because someplace in the program data was written beyond the end of allocated storage. Note, particularly, that the [] index operator for vectors does not check for bounds violations. If somewhere (in the hidden part of your program) you stored something illegally, then School is Out. Undefined behavior can cause unpredictable and hard-to-duplicate (not to mention hard-to-find) errors. Regards, Dave |
|
#3
|
|||
|
|||
Re: 2D Vector Memory ProblemsOne way to help with bounds checking is to use the at() method for vectors. It works just like the [] operator but it throws an out_of_range exception. Like so (uncompiled, untested):
CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
Re: 2D Vector Memory ProblemsAnother approach is declare 2D vector with predefined row and resize it using resize().
|
|
#5
|
|||
|
|||
Re: 2D Vector Memory ProblemsAt lower amounts it works, but at Trainer trainer(10000, 10000, 10000, 10000, 10000) the program crashes on me.
|
|
#6
|
|||
|
|||
Re: 2D Vector Memory ProblemsQuote:
As I browse the results of my simplified test programs I see a total of something on the order of 2 GBytes in the data part of the various vectors for those values. Are you using my example, or have you added some stuff? On my 32-bit Linux systems with GNU compilers, program and data memory can approach 3 GBytes. I feel that is probably true on Debian, since it also uses GNU compilers. Addressable memory on 64-bit Linux systems is a lot more, but I can't recall off of the top of my head what I found the limits to be when I tested it. (GNU compilers on Windows xp have a limit that is closer to 2 GBytes, as I recall). If your program needs more memory than can be addressed by your compiler and operating system, and you don't need much more than a couple of GBytes then you can consider: 1. Get another compiler and/or operating system or 2. Reformulate the problem in such a way that not everything needs to be in memory at the same time. This may not be easy, and, depending on the problem, may not be possible without making run time excessive. or 3. All of the above or 4. Something else. Bottom line: Something that they don't necessarily teach in programming classes (at least not in the beginning) is to estimate the resource requirements before you start writing code. I used the program to tell me, but a careful (manual) analysis could be at least as valuable. Regards, Dave |
|
#7
|
|||
|
|||
Re: 2D Vector Memory ProblemsI tried with both Dev-C++(Bloodshed) which uses Mingw on Windows Vista with a 64-bit Processor AND I tried it on a 32-bit processor machine running Debian Linux using g++.
|
|
#8
|
|||
|
|||
Re: 2D Vector Memory ProblemsI decided to go back to 2D dynamic arrays and have replaced the proprietary stuff with a simple mathematical equation. This should compile fine and if you run it a couple times, it will crash. Please help if you can!
Project.cpp CPP / C++ / C Code:
Trainer.cpp CPP / C++ / C Code:
Trainer.h CPP / C++ / C Code:
Runner.cpp CPP / C++ / C Code:
Runner.h CPP / C++ / C Code:
And the following is the input file with the data randomized: I-Oracle-View1.txt Code:
|
|
#9
|
|||
|
|||
Re: 2D Vector Memory ProblemsQuote:
Assuming that you want Trainer::random() to return a value between zero and one, this following absolutely, incontrovertibly, guaran-dam-teed wrong for any version of GNU compiler: CPP / C++ / C Code:
CPP / C++ / C Code:
I get the following from GNU g++ on my Linux system: Code:
Your Mileage May Vary. Try changing the random() function to CPP / C++ / C Code:
Note that this will return a value that is greater than or equal to zero and less than or equal to one. If you want it to return a value that is greater than or equal to zero and less than one: CPP / C++ / C Code:
There may or may not be other problems, but I hope you see the value of supplying something that we can actually use to compare results with whatever it is that you are running. Regards, Dave |
|
#10
|
|||
|
|||
Re: 2D Vector Memory ProblemsI tested all of my random functions and they work perfectly as is. I will keep your concerns in mind though. The crashing usually happens well before that, anyways.
|
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| IE 7 Problems | colliegirl | Computer Software Forum - Windows | 6 | 05-Jun-2008 09:10 |
| Recent problems with antivirus programmes | steven_symantec | Member Announcements, Advertisements & Offers | 3 | 14-Feb-2008 22:22 |
| Challenge problems plz help plz | crazyABOUTyou | C++ Forum | 3 | 24-Apr-2007 11:32 |
| Chaintech Geforce 5600 FX problems | bartster74 | Computer Hardware Forum | 8 | 04-May-2004 14:16 |
| Chaintech GeForce FX 5600xt Problems | Brymat | Computer Hardware Forum | 14 | 22-Feb-2004 16:45 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The