Thread: Sparse matrix

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    2

    Sparse matrix

    hello. Everybody Im new in programming. Still learning. Currently I have a problem when designing a Sparse Matrix class. Class is based on a linked list which records only the non-zero elements. Source file seems to compile fine, but at a run it crashes. Debug reveals a segmentation fault, which is as far as I know connected with inproper memory allocation. However I don't seem to find the problem. This is just a base class which allows to insert non-zero elements and display them. I am using bloodshed Dev C++. Any help would be appreciated. thanks Milenkom.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Sparse
    {
        private:
            class Elem
            {
                private:
                    int row,column;
                    double*elem;
                    Elem*next;
                public:
                    Elem(){next=NULL;}
                    Elem(int a,int b,double h);
                    ~Elem(){delete elem;}
                    Elem*& Next(){return next;}
                    double& Value()const{return *elem;}
                    int& Row(){return row;}
                    int& Column(){return column;}
            };
            Elem*first;
            int rows;
            int columns;
        public:
            Sparse(int,int);
            ~Sparse();
            void Insert(int a,int b,double h);
            friend ostream& operator<<(ostream&,Sparse&);                 
    };
    
    Sparse::Elem::Elem(int a,int b,double h)
    {
        elem=new double;
        *elem=h;
        next=NULL;
        row=a;
        column=b;   
    }
    
    Sparse::Sparse(int a,int b):rows(a),columns(b)
    {
        
    }
    
    Sparse::~Sparse()
    {
        Sparse::Elem*handy=first;
        Sparse::Elem*prev;
        while(handy!=NULL)
        {
            prev=handy;
            handy=handy->Next();
            delete prev;
        }
    }
    
    
    void Sparse::Insert(int a,int b,double h)
    {
            Sparse::Elem*prvi=new Sparse::Elem(a,b,h);
            Sparse::Elem*handy;
            if(first==NULL)
            {
                first=prvi;
            }
            else
            {
                handy=first;
                while(handy->Next()!=NULL)
                {
                    handy=handy->Next();
                }
                handy->Next()=prvi;
            } 
    }
    
    ostream& operator<<(ostream &os,Sparse &s)
    {
        for(int j=1;j<=s.columns;j++)
        {
            for(int i=1;i<=s.rows;i++)
                if(s.first->Column()==j && s.first->Row()==i)
                {
                    cout<<s.first->Value()<<"  ";
                    s.first=s.first->Next();
                }
                else
                cout<<0<<"  ";
                cout<<"\n";
        }
        return os;
    }
    
    
    int main()
    {
        Sparse s1(10,10);
        s1.Insert(1,1,10);
        s1.Insert(2,1,20);
        s1.Insert(3,2,30);
        cout<<s1;
        system("PAUSE");
        return 0;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It looks like the constructor of Sparse leaves Elem* first uninitialized (it won't be 0).

    There are other strangeness, such as allocating the double dynamically in Elem. Any reason why you don't use the standard containers for the underlying low level stuff?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You could be more consistent with your use of constructor initialiser lists and use them in Elem's constructor too. Initialise all variables that you can this way.
    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
    Jan 2008
    Posts
    2
    Don't worry I already fixed the problem. I also overloaded (int,int) operator which really simplifies the code for ostream<< operator on few lines.

    I could use member initializer list, but its not really necessary. Currently i am still learning, although almost at end. The book i used Primer C++ is very thourough, but the examples are too simple Im afraid. Thats why Im trying to solve more demanding examples (this one is from Sharam Hekmat).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally,
    Code:
    ostream& operator<<(ostream &os,Sparse &s)
    should be:
    Code:
    ostream& operator<<(ostream &os, const Sparse &s)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Sparse Matrix
    By brb9412 in forum C Programming
    Replies: 3
    Last Post: 01-02-2009, 01:12 PM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM