classs with multiple arrays

This is a discussion on classs with multiple arrays within the C++ Programming forums, part of the General Programming Boards category; Hi guys, The following class keeps giving me a segmentation fault. I am trying to dynamically allocate three pointers ptr1, ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    Unhappy classs with multiple arrays

    Hi guys,

    The following class keeps giving me a segmentation fault. I am trying to dynamically allocate three pointers ptr1, ptr2, ptr3. Constant Grids is equal to 20. It works if I have only one pointer.

    Code:
    class matrix_col { 
    
    public:
    
      int i;
      double *ptr1, *ptr2, *ptr3;
    
      matrix_col(){
    
        ptr1 = new double[Grids];
        ptr2 = new double[Grids];
        ptr3 = new double[Grids];
    
      for(i = 0; i < (Grids); i++)
    
        ptr1[i] = 0;
        ptr2[i] = 0;
        ptr3[i] = 0;
    }
    
    //copy constructor
    matrix_col(const matrix_col &a){
    
      int i;
     
        ptr1 = new double[Grids];
        ptr2 = new double[Grids];
        ptr3 = new double[Grids];
    
      for(i = 0; i < (Grids); i++)
    
        ptr1[i] = a.ptr1[i];
        ptr2[i] = a.ptr2[i];
        ptr3[i] = a.ptr3[i];
    }
    
      ~matrix_col(){
    
        delete [] ptr1;
        delete [] ptr2;
        delete [] ptr3;
    
    }
    
      //double index(int, int);
      void show_variable();
    
      //void col_insert(flow_field &, flow_field &, flow_field &);
      friend matrix_col operator+(const matrix_col &, const matrix_col &);
      friend matrix_col operator-(const matrix_col &, const matrix_col &);
      const matrix_col &operator=(const matrix_col &);
    };
    Shuo

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i stopped looking after i noticed a first problem:
    Code:
      matrix_col(){
    
        ptr1 = new double[Grids];
        ptr2 = new double[Grids];
        ptr3 = new double[Grids];
    
      for(i = 0; i < (Grids); i++)
    
        ptr1[i] = 0;
        ptr2[i] = 0;
        ptr3[i] = 0;
    }
    you probably want the three ptr statements enclosed by {}'s. if the problem still persists ill take a deeper look.. hope it helps.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    I added the bracket and it worked as part of a larger code but gives the following error message.

    *** glibc detected *** double free or corruption (!prev): 0x08056b08 ***
    Aborted

    Is it to do with the destruction of the objects?

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    I added some more brackets inside the class functions and the error message went away.
    Thanks for your help

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    yes you needed both brackets.

    what your for loop was doing was running and only executing the ptr1 statement, then once i was 20 it would initialize both 'ptr2[20]' and 'ptr3[20]', which isnt a good idea since your array goes from 0 - (Grids-1)

    your welcome

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Make sure you also implement a copy assignment operator for your class.

    A better solution would be to use a vector rather than a dynamic array. You wouldn't need to worry about either of that function, and you wouldn't have to worry about the copy constructor and destructor either. Those would just work automatically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching Multiple Arrays?
    By Cell in forum C Programming
    Replies: 3
    Last Post: 03-19-2009, 03:45 PM
  2. Global arrays shared between multiple source files
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 08-14-2008, 06:29 PM
  3. Separate long string into multiple arrays
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-27-2007, 01:57 AM
  4. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  5. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 10:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21