GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Mar-2006, 10:33
counterflow counterflow is offline
New Member
 
Join Date: Mar 2006
Posts: 1
counterflow is on a distinguished road

debugging multi-dimensional vector/ matrix class


Hi to all
I am trying to make a multi-dimensional vector/ matrix class: An object on the nth order is composed of (n-1)th order objects. The 0 order object is a float. You might call it a "tensor" class, to be mathematically accurate. Anyway, as far as I have been able to ascertain from various debugging sessions, I have managed to get the collapsible "n-1" order structure right: a constructor for an nth order object of size m calls constructors for (n-1)th order objects m times, until eventually the 0 order objects are created.
But somehow the object that has been initialised correctly by put(int) (I have verified this much) gets lost when exiting get(int), see comments in the code. I get nonsense as output. Can anyone help find the bug? This is way past standard tutorial/ troubleshooting material and I am beginning to get frustrated.

Many thanks in advance!

The class' basic functionality is as follows:


CPP / C++ / C Code:
class matrix
{
    int _ord; int _size; matrix *_mat; float _val; bool _isinit;
    int _totsize;
    public:
        matrix() {}; matrix(matrix &) {};
        matrix(int ord,int size)
        {
            _ord=ord; _size=size; _totsize=int(pow(size,ord+1)); _isinit=true;
            
            if (_ord>0) // decompose
            {
                _mat= new matrix [_size];
                for (int i=0; i<_size; i++)
                {
                    matrix mat(_ord-1,_size);
                    _mat[i]= (matrix&) mat;
                };
            };
            if (_ord==0) {_val=0.;}; // zero order initialised
        };
        int ord(void) {return _ord;}; int size(void) {return _size;};
        int totsize(void) {return _totsize;};
        bool isinit(void) {return _isinit;};  
        
        // submatrix operations
        matrix get(int i) // higher order ***problem?
        {
            return _mat[i]; // at this point _mat[i] is correct
        }; 
        void put(int i, matrix &mat) // tested ok
        {
            _mat[i]=mat;
        };
        
        float get() {return _val;}; // zero order, tested ok
        void put(float val) {_val=val;}; // tested ok
};

    
// test program
int main(int argc, char *argv[])
{
    matrix a(0,1), c(0,1); matrix b(1,1);
    a.put(7.11); c.put(-1.33);  
    b.put(0,a); b.put(1,c); 
    
    cout <<int ((b.get(0)).get()) <<"\t"; // this prints nonsense, why? what happened to the floating point value stored in _mat[]?
    system("PAUSE");
    return 0;
};
  #2  
Old 15-Mar-2006, 12:14
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough

Re: debugging multi-dimensional vector/ matrix class


Your get() method returns a matrix by-value.
CPP / C++ / C Code:
        // submatrix operations
        matrix get(int i) // higher order ***problem?
        {
            return _mat[i]; // at this point _mat[i] is correct
        };
This means that a copy of the sub-matrix is actually returned from the method.

So, let's look at your copy constructor...
CPP / C++ / C Code:
        matrix() {}; matrix(matrix &) {};
It's empty!!! That is, it does nothing beyond the default initializations of the member variables (which in this case is, essentially, nothing). I'd also recommend putting the default ctor and copy ctor declarations on separate lines.

You get nonsense output because the "copied" submatrix contains an uninitialized pointer, and when you dereference it you get garbage.

You may want to return a (possibly const) reference to your submatrix, rather than a temporary copy. In any case, you definitely want to implement your copy ctor (and your default ctor as well).

Matthew
__________________
I was born not knowing and have only had a little time to change that here and there. -- Richard P. Feynman

Boris Podolsky: James! How's the rat business?
James Moreland: Well, actually it's mostly students I'm experimenting on now.
Kurt Gödel: My God, the mazes must be enormous.
 
 

Recent GIDBlogCompress Your Web Site by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic allocation of multi dimensional array pointer C Programming Language 7 13-May-2005 23:50
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 C++ Forum 2 19-Dec-2004 06:06
Help! Some basal questions about MFC xutingnjupt MS Visual C++ / MFC Forum 1 05-Dec-2004 03:38
hashing help saiz66 C++ Forum 1 06-Jul-2004 06:16

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:50.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.