1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

2D Vector Memory Problems

Discussion in 'C++' started by Omega037, Feb 12, 2009.

  1. Omega037

    Omega037 New Member

    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:

    Code:
    #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);

    Code:
    #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:

    Code:
    #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:

    Code:
    #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 Array. Any ideas?
     

Share This Page