Unable to identify error

This is a discussion on Unable to identify error within the C++ Programming forums, part of the General Programming Boards category; I have written a code for creation of a dialog that will display the contents of a tree stored in ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    10

    Unable to identify error

    I have written a code for creation of a dialog that will display the contents of a tree stored in memory. The tree is created by reading data from a file.

    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    #include <QtGui>
    
    using namespace std;
    
    class treenode;
    char *stringtoarray(string);
    struct leveldata
    {
    	string name;
    	double efficiency;
    	long noofflops;
    };
    
    class treenode
    {
    	public:
    		leveldata *data;
    		unsigned int level;
    		treenode *parent;
    		vector <treenode *> children;
    		void createnode(int h, treenode *r)
    		{	
    			level=h;
    			parent=r;
    		}
    		void insertchild(treenode *c)
    		{
    			children.push_back (c);
    		}
    		void initdata(string e, double f, long g)
    		{
    			data->name=e;
    			data->efficiency=f;
    			data->noofflops=g;
    		}
    		treenode* traversal(void)
    		{
    			static vector <treenode*>::size_type p=0;
    			if(p<children.size())
    			{
    				return(children[p]);
    				++p;
    			}
    			else 
    			{
    				p=0;
    				return NULL;
    			}
    		}
    };
    treenode *root=NULL;
    treenode *curr=NULL;
    
    class Mainwindow;
    /*class pushbutton : public QPushButton, public treenode                    //This class enables the ues of right click mouse button
    {
    	Q_OBJECT
    	public:
    		PushButton(QWidget *parent = 0);
    
    	protected:
    		void mouseReleaseEvent(QMouseEvent *e);
    
    	signals:
        		void rightClicked();
    };
    
    pushbutton::pushbutton(QWidget *parent) : QPushButton(parent) {}
    
    void pushbutton::mouseReleaseEvent(QMouseEvent *e) 
    {
    	if (e->button() == Qt::RightButton)
            {	
    		emit rightClicked();
    	}
    	QPushButton::mouseReleaseEvent(e);
    }
    
    void pushbutton::rightClicked()
    {*/
    	
    class Mainwindow : public QDialog, public treenode
    {
            Q_OBJECT
            public:
                    Mainwindow();
    		void createbutton(treenode * node);
    		void adjustroot(QPushButton *button);
            protected:
                    void closeevent(QCloseEvent *event);
            public slots:
                    void displayinfo(treenode  *node);
                    void displaychildren(treenode *node);
            private:     
                    QLabel *namelabel;
                    QLabel *efflabel;
                    QLabel *flopslabel;
                    QTextEdit *namefield;
                    QTextEdit *efffield;
                    QTextEdit *flopsfield;
    		QHBoxLayout *topleftlayout;
    		QHBoxLayout *bottomleftlayout;
    		QVBoxLayout *leftlayout;
    		QVBoxLayout *rightlayout;
    		QHBoxLayout *mainlayout;
    		QScrollArea *scrollbox;
    		QWidget *scrollwidget;
    		QHBoxLayout *scrollboxlayout;
    		QRect Rect;
    		float factor;
    };
    
    Mainwindow::Mainwindow(void)
    {
    	topleftlayout=new QHBoxLayout;
    	bottomleftlayout=new QHBoxLayout;
    	leftlayout=new QVBoxLayout;
    	rightlayout=new QVBoxLayout;
    	mainlayout=new QHBoxLayout;
    	namefield=new QTextEdit;
    	efffield=new QTextEdit;
    	flopsfield=new QTextEdit;
    	treenode *tempnode;
    	tempnode=root->traversal();
    	scrollbox=new QScrollArea;
    	scrollbox->setWidgetResizable(true);
    	bottomleftlayout->addWidget(scrollbox);
    	scrollbox->setFrameShape(QFrame::NoFrame);
    	scrollwidget=new QWidget();
    	scrollboxlayout=new QHBoxLayout(scrollwidget);
    	scrollbox->setWidget(scrollwidget);
    	scrollbox->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    	scrollboxlayout->setAlignment(Qt::AlignLeft);
    	scrollboxlayout->setSizeConstraint(QLayout::SetMinimumSize);
    	while(tempnode!=NULL)
    	{
    		createbutton(tempnode);
    		tempnode=root->traversal();
    	}
    	Rect=QApplication::desktop()->screenGeometry();
    	factor=(float)(Rect.width()/Rect.height());
    	setMaximumSize(Rect.width()*0.7/factor, Rect.height()*0.4/factor);
    	leftlayout->addLayout(topleftlayout);
    	leftlayout->addLayout(bottomleftlayout);
    	mainlayout->addLayout(leftlayout);
    	mainlayout->addLayout(rightlayout);
    }
    
    void Mainwindow::createbutton(treenode *node)
    {
    	QPushButton *newbutton =new QPushButton;
    	newbutton->setDefault(false);
    	newbutton->setEnabled(true);
    	if(node==root)
    	{
    		topleftlayout->addWidget(newbutton);
    	}
    	else
    	{
    		scrollboxlayout->addWidget(newbutton);
    	}
    	QObject::connect(newbutton, SIGNAL(pressed()), this, SLOT(displaychildren(node))); 
    	QObject::connect(newbutton, SIGNAL(pressed()), this, SLOT(displayinfo(node)));
    	QObject::connect(newbutton, SIGNAL(pressed()), this, SLOT(adjustroot(newbutton)));
    	QObject::connect(newbutton, SIGNAL(released()), this, SLOT(displaychildren(node->parent)));
    	QObject::connect(newbutton, SIGNAL(released()), this, SLOT(displayinfo(node->parent)));
    	QObject::connect(newbutton, SIGNAL(released()), this, SLOT(adjustroot(node->parent)));
    }
    
    void Mainwindow::displaychildren(treenode *node)
    {
    	treenode *tempnod;
    	tempnod=node->traversal();
    	scrollbox->setWidgetResizable(true);
    	bottomleftlayout->addWidget(scrollbox);
    	scrollbox->setFrameShape(QFrame::NoFrame);
    	scrollbox->setWidget(scrollwidget);
    	scrollbox->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    	scrollboxlayout->setAlignment(Qt::AlignLeft);
    	scrollboxlayout->setSizeConstraint(QLayout::SetMinimumSize);
    	while(tempnod!=NULL)
    	{
    		createbutton(tempnod);
    		tempnod=root->traversal();
    	}
    	Rect=QApplication::desktop()->screenGeometry();
    	factor=(float)(Rect.width()/Rect.height());
    	setMaximumSize(Rect.width()*0.7/factor, Rect.height()*0.4/factor);
    	leftlayout->addLayout(topleftlayout);
    	leftlayout->addLayout(bottomleftlayout);
    	mainlayout->addLayout(leftlayout);
    	mainlayout->addLayout(rightlayout);
    }
    
    void Mainwindow::displayinfo(treenode *node)
    {
    	namefield->insertPlainText(stringtoarray(node->data->name));
    	namefield->setDocumentTitle("Name:");
    	rightlayout->addWidget(namefield);
    	stringstream ss(stringstream::in|stringstream::out);
    	ss<<node->data->efficiency;
    	string s=ss.str();
    	QString q1(s.c_str());
    	efffield->insertPlainText(q1);
    	efffield->setDocumentTitle("Efficiency:");
    	rightlayout->addWidget(efffield);
    	s.clear();
    	ss.str("");
    	ss<<node->data->noofflops;
    	s=ss.str();
    	QString q2(s.c_str());
    	flopsfield->insertPlainText(q2);
    	flopsfield->setDocumentTitle("Flops:");
    	rightlayout->addWidget(flopsfield);
    }	
    	
    
    void Mainwindow::adjustroot(QPushButton *button)
    {
    	topleftlayout->addWidget(button);
    }						
    	
    /*void treenode::createnode(leveldata *a, int h, treenode *r)
    {
    	data=a;	
    	level=h;
    }
    
    void treenode::insertchild(treenode *c)
    {
    	children.push_back (c);
    }
    
    void treenode::initdata(char *e, double f, long g,)
    {
    	name=e;
    	efficiency=f;
    	noofflops=g;
    }*/
    
    int main(int argc, char *argv[])
    {
    	unsigned int spaces=0;
    	string temp;
    	string temp2;
    	string levname;
    	double eff;
    	long flops;
    	int flag=1;	
    	ifstream myfile("myfile.txt");
    	while(flag)
    	{
    		//Separating the data from the line read to initialise the data members of the object
    		unsigned int pos=spaces;	
    		for(; temp[pos]!=' '; ++pos)
    		{
    			temp2.push_back(temp[pos]);
    		}
    		++pos;
    		levname=temp2;
    		temp2.clear();
    		for(; temp[pos]!=' '; ++pos)
    		{
    			temp2.push_back(temp[pos]);
    		}
    		++pos;
    		eff=atof(stringtoarray(temp2));
    		temp2.clear();
    		for(; pos<temp.length(); ++pos)
    		{
    			temp2.push_back(temp[pos]);
    		}
    		++pos;
    		flops=atol(stringtoarray(temp2));
    		temp2.clear();
    		//Creating a new node
    		treenode *newnode=new treenode;
    		newnode->initdata(levname, eff, flops);
    		newnode->createnode(spaces, curr);
    		if(!newnode->level)
    		{
    			root=newnode;
    		}
    		//Associating the child to the parent
    		curr->insertchild(newnode);
    		//Updating the curr pointer to point to the parent for next node to be created 
    		if (getline(myfile, temp))
    		{
    			flag=1;
    		}
    		else
    		{
    			flag=0;
    			break;
    		}
    		spaces=0;
    		while(temp[spaces]==' ')
    		{
    			++spaces;
    		}
    		if(spaces>newnode->level)
    		{
    			curr=newnode;
    		}
    		else if(spaces<newnode->level)
    		{
    			treenode *temporary;
    			temporary=newnode;
    			while(!(temporary->level<spaces))
    			{
    				temporary=temporary->parent;
    			}
    			curr=temporary;
    		}
    	}
    	QApplication app(argc, argv);
    	Mainwindow *mywindow=new Mainwindow;
    	mywindow->show();
    	return app.exec();
    }
    
    char *stringtoarray(string s)
    {
    	char *a=new char[s.size()+1];
    	memcpy(a, s.c_str(), s.size());
    	a[s.size()]='\0';
    	return a;
    }
    		
    #include "entrytree2.moc"
    Here is the file dat I'm reading :
    Code:
    a 48 23000
     b 89 4500
      c 40 2200
      d 94 2300
     e 39 12200
      f 47 6250
       g 98 3250
       h 23 3000
      i 88 5950
       j 98 2590
       k 80 2400
     l 29 6300
      m 70 4300
      n 12 2000
    I am getting a segmentation fault on running the code. Debugging using gdb gives the following error
    Code:
    #0  0x0804caa1 in main ()
    I'm unable to understand what this means. I tried running the program in gdb and using where but dat didn't help. Can someone please help me out.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The line you posted means almost nothing at all (it means your program started in main, which I was pretty confident about even without the trace). All the lines around it, especially the one at the very top, are the important ones as they tell you where your program had gotten to (as opposed to where it had started).

    Also if you are going to run your program in gdb you need to compile your program with the -g switch.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    @tabstop..................I am generating the makefile using qmake.............how should I apply the -g switch ?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Add it to all your other switches.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    @tabstop...........this is the first time I'm working with gc++ compiler, so I'm unable to get understand working with switches. Can you please elaborate ??

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I just searched on "qmake" and found this:
    Quote Originally Posted by qmake tutorial
    Making an Application Debuggable

    The release version of an application doesn't contain any debugging symbols or other debugging information. During development it is useful to produce a debugging version of the application that has the relevant information. This is easily achieved by adding debug to the CONFIG variable in the project file.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    @tabstop, thanks for the last hint, it did work................now on debugging I'm getting :

    Code:
    #0  0x0804c58d in main (argc=1, argv=0xbfffe7d4) at entrytree2.cpp:263
    263                     for(; temp[pos]!=' '; ++pos)
    I added a getline just before the while(flag) as I thought this may be due to attempt to access empty string, but this didn't solve the problem. Any suggestions??

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    @tabstop, thanks for the last hint, it did work................now on debugging I'm getting :

    Code:
    #0  0x0804c58d in main (argc=1, argv=0xbfffe7d4) at entrytree2.cpp:263
    263                     for(; temp[pos]!=' '; ++pos)
    This line refers to the first for loop inside the while(flag) loop. I added a getline just before the while(flag) as I thought this may be due to attempt to access empty string, but this didn't solve the problem. Any suggestions??

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And when you get to this point what is the value of temp? and pos?

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    pos = 20604

    Code:
    temp={static npos = 4294967295, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0xb7295f84 ""}}

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If this was the first time through, I would expect pos to be 0, but maybe this is you just finally walking off the end of the string or something. I think you need the whole bit from if (getline) all the way to the end to appear before you start trying to do things like use the value of temp.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    I restructured the main to look like :
    Code:
    int main(int argc, char *argv[])
    {
    	size_t spaces=0;
    	size_t nextspaces=0;
    	size_t pos=0;
    	string temp;
    	string temp2;
    	string levname;
    	double eff;
    	long flops;
    	int flag=1;	
    	ifstream myfile("sample.txt");
    	do
    	{
    		if(getline(myfile, temp))
    		{
    			flag=1;
    		}
    		else
    		{
    			flag=0;
    			break;
    		}
    		//Separating the data from the line read to initialise the data members of the object	
    		for(; temp[pos]!=' '; ++pos)
    		{
    			temp2.push_back(temp[pos]);
    		}
    		++pos;
    		levname=temp2;
    		temp2.clear();
    		for(; temp[pos]!=' '; ++pos)
    		{
    			temp2.push_back(temp[pos]);
    		}
    		++pos;
    		eff=atof(stringtoarray(temp2));
    		temp2.clear();
    		for(; pos<temp.length(); ++pos)
    		{
    			temp2.push_back(temp[pos]);
    		}
    		++pos;
    		flops=atol(stringtoarray(temp2));
    		temp2.clear();
    		//Creating a new node
    		treenode *newnode=new treenode;
    		newnode->initdata(levname, eff, flops);
    		newnode->createnode((unsigned int)spaces, curr);
    		if((!newnode->level)||(spaces==0)
    		{
    			root=newnode;
    			curr=newnode;
    		}
    		//Associating the child to the parent
    		curr->insertchild(newnode);
    		//Updating the curr pointer to point to the parent for next node to be created 
    		if (getline(myfile, temp))
    		{
    			flag=1;
    		}
    		else
    		{
    			flag=0;
    			break;
    		}
    		spaces=0;
    		while(temp[spaces]==' ')
    		{
    			++spaces;
    		}
    		if(spaces>newnode->level)
    		{
    			curr=newnode;
    		}
    		else if(spaces<newnode->level)
    		{
    			treenode *temporary=newnode;
    			while(!(temporary->level<spaces))
    			{
    				temporary=temporary->parent;
    			}
    			curr=temporary;
    		}
    		pos=spaces;
    	}while(flag==1);
    	QApplication app(argc, argv);
    	Mainwindow *mywindow=new Mainwindow;
    	mywindow->show();
    	return app.exec();
    }
    However the debugger still returns the same problem and the same values of pos and temp. Also the value of flag is 1. I'm unable to understand the problem

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Well the loop will keep going until it finds a space in the string right... What happens if there is no space before the end of the string?

    Answer: It keeps going, past the end of the string, out into the wild untamed no-mans-land, where *boom*, it eventually smacks into an area of memory that the OS knows it never gave to the program, at which point the OS goes *slap* bad program and terminates it.
    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"

  14. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    @iMalc, I agree with you. But the file (I have mentioned the contents in the first post of the thread) does contain spaces. So I'm unable to understand why the loop is not exiting.

  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    I was able to resolve the previous error. However now I see a different error

    Code:
    #1  0xb7242b94 in std::string::operator= (this=0x0, __str=@0xbfffe704) at /opt/data/Download/gcc-4.2.2/i686-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:491
    491     in /opt/data/Download/gcc-4.2.2/i686-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h
    can anyone tell me what this means??

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help. Can't identify error.
    By bummielove in forum C Programming
    Replies: 23
    Last Post: 07-05-2011, 06:06 AM
  2. getting error "unable to open include stdio.h
    By coolgal in forum C Programming
    Replies: 18
    Last Post: 12-07-2007, 12:36 AM
  3. (Error message) Fatal: Unable to open file 'C.OBJ'
    By drojen in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2006, 12:05 AM
  4. can you identify this car?
    By axon in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-19-2004, 02:59 PM

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