I 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:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string.h>
#include <time.h>
#include <math.h>
#include <vector>
using namespace std;
class Runner {
private:
int num_gates;
int num_models;
int num_trainings;
int total;
vector< vector<float> > training_data; //array of training pairs
public:
Runner();
Runner(int n_gates, int n_models, int n_trainings, char *train_file); //constructor
~Runner(); //destructor
float compute_output();
vector< vector<float> > load_training_data(); //loads training data
void setsigmas(vector<float> sig);
#include "Runner.h"
class Trainer {
private:
vector<vector<float> > sig_data;
int population_size;
int num_generations; //number of generations
Runner g;
int num_trainings; //number of training pairs
int num_gates; //number of gate variables
int num_models; //number of models
void save_pop();
vector<vector<float> > load_pop();
vector<float> mutcross_sig;
void mutcross();
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:
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?
Ridiculous claim that the labor cost difference of $6000 annually would increase cost per phone by $200. The employees produce 3 phones per month or what?
Sparkle 2.20.1 by Razvan Serea
Sparkle is a free, open-source Windows optimization tool designed to make your PC faster, cleaner, and more private. With Sparkle, you can easily debloat Windows by removing unnecessary apps and services, disable Microsoft tracking to enhance privacy, and apply performance tweaks to boost speed. Its cleaner removes junk and temporary files, while every change is safe and fully reversible. Sparkle also features a modern, user-friendly interface with automatic updates, making system maintenance simple. Explore over 39 tweaks, from disabling telemetry and hibernation to optimizing network and game settings, all aimed at customizing and enhancing your Windows experience.
Sparkle supports Windows 10 and 11.
Sparkle 2.20.1 changelog:
You can now change the Animation Direction from Up, Left, or Off.
Added configurable animation direction (Up, Left, Off) for improved accessibility
Added TTL caching to the system info backend
Refactored tweak application flow to await NvidiaProfileInspector
Improved IPC listener cleanup to correctly remove specific listeners
Fixed online status not updating after successful network requests
Updated system info tests to support backend caching
Removed electron-toolkit utils dependency in favor of internal is.dev helper
Fixed unwanted files and folders being included in application bundles
Download: Sparkle 2.20.1 | Portable | ~100.0 MB (Open Source)
Links: Sparkle Website | Github | Screenshot
Get alerted to all of our Software updates on Twitter at @NeowinSoftware
And I just bought a seat cushion for my mesh chair. The chair feels nice but the first time I sat in it with boxers, I realized I don't like the feel of mesh on my legs. 😂
Question
Omega037
I 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:
#include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #include <string.h> #include <time.h> #include <math.h> #include <vector> using namespace std; class Runner { private: int num_gates; int num_models; int num_trainings; int total; vector< vector<float> > training_data; //array of training pairs public: Runner(); Runner(int n_gates, int n_models, int n_trainings, char *train_file); //constructor ~Runner(); //destructor float compute_output(); vector< vector<float> > load_training_data(); //loads training data void setsigmas(vector<float> sig);#include "Runner.h" class Trainer { private: vector<vector<float> > sig_data; int population_size; int num_generations; //number of generations Runner g; int num_trainings; //number of training pairs int num_gates; //number of gate variables int num_models; //number of models void save_pop(); vector<vector<float> > load_pop(); vector<float> mutcross_sig; void mutcross();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:
#include "Runner.h" Runner::Runner(){ } Runner::Runner(int n_gates, int n_models, int n_trainings){ num_gates = n_gates; num_models = n_models; num_trainings = n_trainings; total = n_gates + n_models + 1; try{ training_data = vector<vector<float> >(n_trainings, vector<float> (total) ); } catch (bad_alloc) { cout << "PROBLEM"; } training_data = load_training_data(); } vector<vector<float> > Runner::load_training_data() { ifstream c("test1.txt"); float abc; vector< vector<float> > t_data; vector<float> def; for(int i = 0; i < num_trainings; i++){ for(int j = 0; j < total; j++){ c >> abc; def.push_back(abc); } t_data.push_back(def); def.clear(); } return t_data; }This is the constructor and load data for the Trainer class:
#include "Trainer.h" Trainer::Trainer(int pop_size, int num_gen, int n_trainings, int n_gates, int n_models){ total = n_gates +1; float t_sig[total-1]; top=1000; char savefile[40]; float holdB; int nogo=0; //set the parameters population_size = pop_size; num_generations = num_gen; num_trainings = n_trainings; num_gates = n_gates; num_models = n_models; cout <<"Save To Filename" << endl; cin >> savefile; pop.open(savefile); gen_report.open("abctre4.txt"); g = Runner(num_gates, num_models, num_trainings); int load=0; cout << "Load Data? (1=YES, 0=NO)"; cin >> load; if(load == 1){ sig_data = load_pop(); gen_best=0; gen_avg=0; } else{ for(int t=0;t<pop_size;t++){ sig_data.push_back(vector<float>()); } for(int y=0; y < pop_size; y++){ for(int j=0; j < num_gates; j++){ holdB=random(min_value, max_value); try{ sig_data[y].push_back(holdB); } catch(bad_alloc){ cout << " MEMLIMIT "; try{ sig_data[y].push_back(holdB); } catch(bad_alloc){ cout << " MEMLIMIT_AGAIN "; nogo=1; } } t_sig[j]=holdB; } g.setsigmas(t_sig); holdB=g.crossvalidation_run(0); sig_data[y].push_back(holdB); if(holdB <= top){ gen_best = y; top = holdB; } if(nogo == 0){ gen_avg = gen_avg + holdB; } else{ gen_avg = gen_avg + (gen_avg/y+1); nogo = 0; } } } for(int x=0; x < num_gates; x++){ try{ mutcross_sig.push_back(0); } catch(bad_alloc){ cout << " MUTMEMLIMIT "; try{ mutcross_sig.push_back(0); } catch(bad_alloc){ cout << " MUTMEMLIMIT_AGAIN "; } } } } /***************************************************************/ vector<vector<float> > Trainer::load_pop() { char loadfile[40]; cout <<"Load From Filename" << endl; cin >> loadfile; ifstream inpop(loadfile); float abc; vector< vector<float> > s_data; vector<float> indef; for(int i = 0; i < population_size; i++){ for(int j = 0; j < total; j++){ inpop >> abc; indef.push_back(abc); } s_data.push_back(indef); indef.clear(); } inpop.close(); return s_data; }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?
Link to comment
https://www.neowin.net/forum/topic/734718-help-with-2d-vector-memory-errors/Share on other sites
0 answers to this question
Recommended Posts