LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to use pointer to an array of pointers on CPP? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-use-pointer-to-an-array-of-pointers-on-cpp-814367/)

Lobinho 06-15-2010 06:34 PM

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

David1357 06-15-2010 08:05 PM

Quote:

Originally Posted by Lobinho (Post 4004794)
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.

Lobinho 06-16-2010 07:51 AM

Thanks David1357!

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


All times are GMT -5. The time now is 04:28 AM.