2-dimensional array, size limitations?



  • 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.


  • Lifetime Qt Champion

    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.



  • 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?


  • Lifetime Qt Champion

    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 ?



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



  • 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);
    }@


  • Moderators

    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



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


  • Moderators

    [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 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 ;)


  • 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.



  • 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.


  • Lifetime Qt Champion

    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.


Log in to reply
 

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