C++ program runs in linux but not windows -


i writing program college , keep having problems it. wrote in using codeblocks in ubuntu , runs fine no errors or anything. when run in windows on codeblocks crashes , keep getting same error "terminate called after throwing instance of 'std::bad_alloc' what<>: std::badalloc' stops working. appreciated! thankyou ron

main.cpp

 #include <iostream> #include "set.h" using namespace std;  int main() {  set set1; list list1; list1.header();   int choice = 0; int value = 0; cout<<"press 1 use list or press 2 set"<<endl; cin>>choice; if(choice == 1) //list {      while(choice != 4)         {             value = 0;             list1.menu();             cin>>choice;             switch(choice)             {                 case 1:cout<<"please enter value"<<endl;                        cin>>value;                        list1.set_value(value);                 break;                 case 2:list1.print_list();                 break;                 case 3:list1.test_copy_constructor();                 break;             }          } }  else if(choice == 2) //set {      while(choice != 4)         {              value = 0;             list1.menu();             cin>>choice;             switch(choice)             {                 case 1:cout<<"please enter value"<<endl;                        cin>>value;                        set1.set_value(value);                 break;                 case 2:set1.print_list();                 break;                 case 3:set1.test_copy_constructor();                 break;             }          }  }  else {     cout<<"please enter valid option"<<endl; }   return 0; } 

set.cpp

#include "set.h" #include <iostream> #include <string>  using namespace std;  //constructor list::list() {   int array_size;   int *array = new int[array_size];   // delete [] array;  } list list1;    //print functions void list::header(void) const {     cout<<"program name: program 2"<<endl;     cout<<"program created: march 20,2014"<<endl;     cout<<"created by: ron miller"<<endl;     cout<<"--------------------------------"<<endl;     cout<<"            "<<endl; }   void list::menu(void) const {        cout<<"                         menu"<<endl;     cout<<"---------------------------------------------------------"<<endl;     cout<<"1. insert (value inserted entered keyboard)"<<endl;     cout<<"2. print list (all values, 1 per line)"<<endl;     cout<<"3. test copy constructor (pass list value function"<<endl;     cout<<"   , within function change values in list"<<endl;     cout<<"   0, call print list before function ends)"<<endl;     cout<<"4. quit"<<endl;     cout<<"---------------------------------------------------------"<<endl;  }  //modification functions void list::set_value(const int value) {     if (slot == 0) //if first run set array size         {         array = new int[array_size];         }      if (slot == array_size) //if array needs extended  save data in temp array expand original array copy original         {         cout<<"expand array"<<endl;         temp_array = array;         array_size = array_size + 2;         array = new int[array_size];         array = temp_array;         }      array[slot] = value; //set current array slot value     slot = slot+1; }   void list::print_list(void) const { int = 0;  cout<<"---------------"<<endl;  while(i < slot)         {         cout<<array[i]<<endl;         = i+1;         }  cout<<"---------------"<<endl; }  void list::test_copy_constructor(void) const { int* test_array; test_array = array; int = 0; test_array = new int[array_size]; //copy original array test_array  while(i < slot)                  //set array 0         {         test_array[i] = 0;         = i+1;         }  = 0;   cout<<"---------------"<<endl; while(i < slot)                  //remove testing purposes         {         cout<<test_array[i]<<endl;         = i+1;         }         = 0;  cout<<"---------------"<<endl; list::print_list();  //print original array }  void set::set_value(const int value) {     array = get_array();        //use functions obtain copies of private data     temp_array = get_temp_array();     array_size = get_array_size();     temp_array_size = get_temp_array_size();      slot = get_slot();     char match;       match = set::search_array(value);      if(match == 'y')      {         cout<<"match"<<endl;      }       if(match == 'n')      {          if (slot == 0) //if first run set array size             {             array = new int[array_size];             }          if (slot == array_size) //if array needs extended  save data in temp array expand original array copy original             {             cout<<"expand array"<<endl;             temp_array = array;             array_size = array_size + 2;             array = new int[array_size];             array = temp_array;             }         array[slot] = value; //set current array slot value         slot = slot+1;         set_array(array);          //use set values update private data         set_temp_array(temp_array);         set_array_size(array_size);         set_temp_array_size(temp_array_size);         set_slot(slot);     } } char set::search_array(int value) {     array = get_array();     array_size = get_array_size();     slot = get_slot();     int array_value;     char match = 'n';     int =0;     while(i < slot)          //searches array match if there return y otherwise return n         {             if( array[i] == value)             {                 match = 'y';             }             else             {                 match = 'n';             }         = i+1;         }     return match; }  //set functions void list::set_array(int* value) {     array = value; } void list::set_array_size(int value) {     array_size = value; } void list::set_temp_array(int* value) {     temp_array = value; } void list::set_temp_array_size(int value) {     temp_array_size = value; } void list::set_slot(int value) {     slot = value; } //get functions  int* list::get_array(void) const {     return array; } int* list::get_temp_array(void) const {     return temp_array; }  int list::get_array_size(void) const {     return array_size; } int list::get_temp_array_size(void) const {     return temp_array_size; }  int list::get_slot(void) const {     return slot; } 

set.h

#ifndef set_h_included #define set_h_included   class list {     public:         //constructor         list();          //print functions         void header (void) const;         void menu (void) const;          //modification functions         void set_value (const int);         void print_list(void) const;         void test_copy_constructor(void) const;           //set functions         void set_array( int*);         void set_temp_array(int*);         void set_array_size( int);         void set_temp_array_size(int);         void set_slot(const int);         //get functions         int* get_array (void) const;         int* get_temp_array (void) const;         int get_array_size (void) const;         int get_temp_array_size (void) const;         int get_slot(void) const;       private:        int array_size;        int *array;        int *temp_array;        int temp_array_size;        int slot = 0;   };  class set : public list { public:       //modification functions       void set_value (const int);       char search_array(const int);  private:       int array_size = 2;       int *array;       int *temp_array;       int temp_array_size = 2;       int slot = 0;   };   #endif 

list::list() {    int array_size;    int *array = new int[array_size];    // ... } 

what value array_size supposed have? how large int array supposed allocated?

it seems me local variable declaration superfluous; remove it, , use member variable, instead. (you've initialised member variable 2 @ point of declaration, using new c++11 feature allows variable not static const.)

don't forget hand allocated memory when you're done it. in general propose use std::vector this.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -