Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to create QList of arrays

How to create QList of arrays

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BasicPoke
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

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

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BasicPoke
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved