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. 2-dimensional array, size limitations?
Forum Updated to NodeBB v4.3 + New Features

2-dimensional array, size limitations?

Scheduled Pinned Locked Moved General and Desktop
13 Posts 5 Posters 7.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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    Sure there is, it's a two phase process.

    Allocate the first dimension

    Use a for loop to allocate each element of the second dimension

    You can also search for multidimensional array allocation if you need more details.

    But since you seem not be comfortable with this exercise. Are you sure that a 2 dimensional vector is not a better solution ?

    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
    • C Offline
      C Offline
      Code_ReaQtor
      wrote on last edited by
      #5

      Might be better if you use Qt Container classes for that. Just a suggestion.

      Please visit my open-source projects at https://github.com/Code-ReaQtor.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Artemus
        wrote on last edited by
        #6

        How do i create a 2D QVector on heap? this doesn't work:
        @ QVector< QVector<uint8> >* Vector1 = new QVector< QVector<uint8> >(4032);
        QVector< QVector<uint8> >* Vector2 = new QVector< QVector<uint8> >(4032);
        for(int a=0; a<4032; a++)
        {
        Vector1[a].resize(3024);
        Vector2[a].resize(3024);
        }@

        and this doesn't compile:
        @ QVector< QVector<uint8>* >* Vector1 = new QVector< QVector<uint8>* >(4032);
        QVector< QVector<uint8>* >* Vector2 = new QVector< QVector<uint8>* >(4032);
        for(int a=0; a<4032; a++)
        {
        Vector1[a] = new QVector<uint8>(3024);
        Vector2[a] = new QVector<uint8>(3024);
        }@

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #7

          QVector's internal data is automatically allocated the heap, even if the QVector object is on the stack.

          [quote]
          @
          QVector< QVector<uint8> >* Vector1 = new QVector< QVector<uint8> >(4032);
          @
          [/quote]You created a 3D vector here (that's a 1D-array containing 4032 2D-QVectors). Remember that these are basically the same:

          • char argc[][]
          • char *argc[]
          • char **argc

          Just write QVector<QVector<uint8>> Vector1

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KeithS
            wrote on last edited by
            #8

            Don't forget to add a space between the >> in the above else your compiler will think it's a right shift operator!

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #9

              [quote author="KeithS" date="1376649728"]Don't forget to add a space between the >> in the above else your compiler will think it's a right shift operator![/quote]Not if your compiler is using C++11 :)

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KeithS
                wrote on last edited by
                #10

                [quote author="JKSH" date="1376662589"][quote author="KeithS" date="1376649728"]Don't forget to add a space between the >> in the above else your compiler will think it's a right shift operator![/quote]Not if your compiler is using C++11 :)

                [/quote]

                • and all the bugs have been fixed ;)
                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Artemus
                  wrote on last edited by
                  #11

                  I wrote a small test program today to compare 1D Array with the 2D Vector. A bicubic Interpolation of 4032x3024 Pixel takes 7,748 milliseconds with 1D Array and 24,594 milliseconds with the 2D Vector. Quite obvious what I will use. In this project i don't need the extra functionality that QVector offers.

                  Thanks for your help.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    KeithS
                    wrote on last edited by
                    #12

                    Simple fact is that if you are seeking performance, you can often do better by writing your own class than by using Qt/STL classes. Of course there is a tradeoff of usability/robustness against performance, but that's your choice.

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

                      You can also try with a 1D QVector.

                      QVector also allows you to access it's data directly so you gain automatic memory management by QVector and you can still access the raw data for computation.

                      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