How to create QList of arrays



  • Hello, I need to create a QList of arrays. Following is what I have come up with. Does this look ok? Should I call _delete _ here or not? Is there a shorthand for this other than the loop?

    @
    QList<unsigned short *>SplitData;
    unsigned short *pTemp;
    for( unsigned int j=0; j<NumGauges; j++ )
    {
    pTemp = new unsigned short[BufSize];
    SplitData.append( pTemp );
    delete pTemp;
    }@

    Thanks.
    Ron



  • Hi,

    You shall NOT call delete because you're inserting in SplitData list only a pointer (not copy of data)



  • Thanks mcosta. What if the above were inside a function, and the list SplitData were passed into the function. Would the list items still be available after the function returned? Or not since the pointers have gone out of scope?

    I should 'delete' each element later after I have used it, right? I suppose clearing the list will not take care of that.
    Ron


  • Lifetime Qt Champion

    Hi,

    Depends on how you call the function. If you have a pointer that goes out of scope you loose the access to the memory segment you just allocated. In your case you have a copy of the pointer in your list so you still can access (and thus delete) it.

    qDeleteAll to the rescue :)

    Hope it helps


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.