• 0

Help with 2D Vector Memory Errors


Question

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

There have been no answers to this question yet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • We had no idea as kids how much time and energy it took to be an adult 😅
    • The Trump administration doesn't want you to use OpenAI's GPT-5.6 without its approval by David Uzondu Image via @realDonalTrump (X) As OpenAI prepares the release of its next model, GPT 5.6, the White House has instructed the company to limit the distribution of the software to a small group of government-approved partners instead of the general public, as it has done with previous releases. According to The Information, OpenAI Chief Executive Officer Sam Altman sent an internal memo to staff on Thursday explaining that the federal government will approve access "customer by customer" during an initial preview phase. Altman noted in the communication that this restrictive rollout is "not [their] long-term model" for software deployment, and the company plans to work toward a "more sustainable" distribution method later. CNN said that both OpenAI and the Trump administration view the capabilities of GPT 5.6 on the same level as Anthropic's Mythos and that government officials intend to "collaborate with frontier AI labs to develop shared approaches for addressing the challenges of scaling this technology." The latest restriction comes just weeks after the US Commerce Department decided to restrict Fable, a version of Mythos with extra safety "guardrails" to prevent users from exploiting software vulnerabilities. Not long after the release, though, researchers at Amazon found a way to bypass these restrictions, prompting an aggressive response from federal authorities. The government ordered Anthropic to cut off access for non-US citizens located outside the US, non-US citizens living inside the US, and incredibly, even Anthropic's own foreign-born employees. Anthropic now appears to be building a workaround to resolve this compliance block with an update to its Privacy Policy that introduces a category called "Verification Data" to handle KYC and Digital IDs. This setup could mandate digital identity checks to filter users by nationality, requiring a government-issued ID and facial biometric data. Who knows? Maybe in the future, you would have to scan your US Passport or State ID to prove your citizenship before you are allowed to chat with Fable 5 (or any other model).
    • When Windows 7 was released I created an AutoHotkey script that uses Alt+` as a keyboard shortcut to move a window across monitors. I have been using that script for over 15 years and this is the first time I have come across another app that uses the same shortcut!
  • Recent Achievements

    • Week One Done
      xvvxcvv earned a badge
      Week One Done
    • One Month Later
      xvvxcvv earned a badge
      One Month Later
    • Enthusiast
      Xonos went up a rank
      Enthusiast
    • Conversation Starter
      Admir earned a badge
      Conversation Starter
    • First Post
      The_Focal_Point earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      400
    2. 2
      +Edouard
      170
    3. 3
      PsYcHoKiLLa
      127
    4. 4
      neufuse
      69
    5. 5
      Xenon
      66
  • Tell a friend

    Love Neowin? Tell a friend!