multi-dimensional array

This is a discussion on multi-dimensional array within the C++ Programming forums, part of the General Programming Boards category; i keep getting a segmentation falut when declaring the an object of the following class. Code: // Declare a class ...

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

    multi-dimensional array

    i keep getting a segmentation falut when declaring the an object of the following class.

    Code:
    // Declare a class for column matricies
    
    class matrix_col{ 
    
     private:
    
     public:
    
      int i, j, k;
      double ptr[4][HoriG][VertG];
    
      //manager functions
    
    //constructor
    
    matrix_col::matrix_col(){
    
      for(k = 0; k < 4; k++) {
    
        for(i = 0; i < HoriG; i++) {
    
          for(j = 0; j < VertG; j++){
    
    	ptr[k][i][j] = 0;
    
          }
        }
      }
    }
    
    //copy constructor
    
    matrix_col::matrix_col(const matrix_col &a){
    
      for(k = 0; k < 4; k++) {
    
        for(i = 0; i < HoriG; i++) {
    
          for(j = 0; j < VertG; j++){
    
    	ptr[k][i][j] = a.ptr[k][i][j];
    
          }
        }
      }
    }
    
    
    //destructor
    
    matrix_col::~matrix_col(){}
    
    }
    Not sure why

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    post the minimal compilable code that gives you segfault.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    That class should not have a destructor. Empty destructors are unnecessary.
    It should also not have an assignment operator because the default generated copy-constructor already is the same as calling the assignment operator for every member variable, and since these are all POD types it's a no-brainer for the compiler to optimise this to the equivalent to a memcpy. By inplementing it yourself you're leaving yourself open to bugs and a slower implementation, and just more unnecessary code.
    i, j, and k should NOT be declared as member variables. They should be local to each function that uses them.

    There is nothing in your constructor that would cause a crash, assuming HoriG and VertG are constants and are a both sensibly small enough numbers. The problem must be in the code with the declaration of the instance. Show the code, and show what line it crashes on.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int i, j, k;
    As iMalc said, there's no reason to make these class members. Just declare them local to each function. However variables for each dimension of the matix (first dimension, HorzG, VertG) might be nice.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    maybe its easier just to declare them as multidimensional arrays.
    double X[3][3][3].

    But then you can't return them from function calls.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Zero out two dimensional array
    By davo666 in forum C Programming
    Replies: 16
    Last Post: 01-08-2009, 04:28 AM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. 2d arrays in C
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 04-20-2002, 11:09 AM
  5. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

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