LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-15-2010, 06:34 PM   #1
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Rep: Reputation: 18
How to use pointer to an array of pointers on CPP?


Hello,

I'm trying to list some information of my database. To do that I'm using a class (MyClassDao) that will do all the SQL stuff and returns to me a list of objects with the content of my table (each row = one object). The problem is: my list gets only the first object... on the second index I'm getting 'segfault'.

Can someone post something to help me (link, reference or example)?

The call for the list function is:
Code:
    MyClassDao* mcd = new MyClassDao();
    MyClass** myClassList;
    MyClass* myClass;
    int size;

    mcd->list2(db, NULL, &myClassList, &size);
    if ((size > 0) && (myClassList[0] != NULL)) {
        for (int i = 0; i < size; i++) {
            myClass = (MyClass*) myClassList[i];
            printf("%d\n", myClass->getId());
            printf("\n");
        }
    }
And the list function:
Code:
int MyClassDao::list2(void* conn, void* obj, MyClass*** list, int* size) {

	int rows; //number of returned lines 
	char*** table; //object that handles the result
	/* DO SQL STUFF */    
		
	*list = new MyClass*[rows];			//creating a new array with "row" size
	*size = rows;						//returning the number of lines
	for (int i = 0; i < rows; i++) {	
		*list[i] = new MyClass();		//creating a new instance

		//Setting the content of line i to my object
		((MyClass*) *list[i])->setId(atoi(table[i][0]));
	}	   
	return 0;
}
Thanks in advance
 
Old 06-15-2010, 08:05 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Lobinho View Post
Can someone post something to help me (link, reference or example)?
Try this
Code:
    MyClassDao* mcd = new MyClassDao();
    MyClass* myClassList;
    MyClass* myClass;
    int size;

    mcd->list2(db, NULL, &myClassList, &size);
    if ((size > 0) && (myClassList != NULL)) {
        for (int i = 0; i < size; i++) {
            myClass = &myClassList[i];
            printf("%d\n", myClass->getId());
            printf("\n");
        }
    }
And this
Code:
int MyClassDao::list2(void* conn, void* obj, MyClass** list, int* size) {

	int rows; //number of returned lines 
	char*** table; //object that handles the result
	/* DO SQL STUFF */    
		
	MyClass *plist = new MyClass[rows];			//creating a new array with "row" size
	*size = rows;						//returning the number of lines
	for (int i = 0; i < rows; i++) {	
		//Setting the content of line i to my object
		plist[i].setId(atoi(table[i][0]));
	}

        *list = plist;

	return 0;
}
It is much more simple, and also solves your pointer problem.
 
1 members found this post helpful.
Old 06-16-2010, 07:51 AM   #3
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Original Poster
Rep: Reputation: 18
Thanks David1357!

It works!
I guess I was making too much confusion with these pointers to object.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Function pointers to class members, cpp brazilnut Programming 2 05-26-2010 10:18 AM
CPP: Multiple pointers to one heap object problem. RHLinuxGUY Programming 2 03-27-2008 09:01 AM
c array of pointers greg108 Programming 10 08-17-2005 10:25 AM
array of char pointers djgerbavore Programming 2 01-08-2005 01:59 PM
cpp: pointers Wonko the Sane Programming 3 11-17-2003 03:38 AM


All times are GMT -5. The time now is 05:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration