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 Update on Monday, May 27th 2025

2-dimensional array, size limitations?

Scheduled Pinned Locked Moved General and Desktop
13 Posts 5 Posters 6.9k Views
  • 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.
  • A Offline
    A Offline
    Artemus
    wrote on last edited by
    #1

    are there general limitations of the size of an 1 or 2-dimensional array in Qt?

    i'am trying to create an array like this:
    @uint8 au8_referenceImage[4032][3024];@

    while 4032x1024 and 4032x2024 works like expected my program crash when using 4032x3024;

    I'am working with Qt 4.8.0 (32 bit) on Ubuntu with 2GB Memory.

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

      Hi,

      This has nothing to do with Qt. You are creating an object of 12192768 bytes so approximatively 12MB on the stack which has not that kind of space to give.

      For this size of arrays you have to allocate it on the heap. An alternative is to use a two dimensional QVector.

      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
      • A Offline
        A Offline
        Artemus
        wrote on last edited by
        #3

        and how can i do this? must be something with "new" right?

        @uint8* au8_referenceImage = new uint8[4032][3024]; @ doesn't work :(

        i could use a one dimensional array instead but isn't there a way with 2D?

        1 Reply Last reply
        0
        • 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