Page 1 of 2 12 LastLast
Results 1 to 20 of 50

Thread: QVector crashes when array size is big

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QVector crashes when array size is big

    6*3000*2000*sizeof(int) = 36M*sizeof(int) = 144 (or 288) MB just for creating the array. Aren't you running out of memory? If not, are you sure C++ copies arrays? I doubt that so copying the struct might lose its integrity.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QVector crashes when array size is big

    The crash is not because it is QVector. You program will crash without QVector in pure C++..

    Don't put such a huge array in stack. You should use memory in the heap. But it is going to swap if you don't have enough memory...

    Do this:
    Qt Code:
    1. struct myStruc
    2. {
    3. QString name;
    4. int *array;
    5.  
    6. myStruc() : array( new int[ 3000*2000] ) {
    7. }
    8.  
    9. // or with size argument in constructor
    10. myStruct( size_t sz ) : array( new int[ sz ] ) {
    11. }
    12.  
    13. ~myStruc() {
    14. delete [] array;
    15. }
    16.  
    17. };
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QVector crashes when array size is big

    Actually QVector allocates its data on the heap even if the vector object itself is on the stack.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QVector crashes when array size is big

    Quote Originally Posted by wysota View Post
    Actually QVector allocates its data on the heap even if the vector object itself is on the stack.
    QVector is fine. He has a huge array in the stack, exceeding stack limit.

  5. #5
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    Quote Originally Posted by lni View Post
    QVector is fine. He has a huge array in the stack, exceeding stack limit.
    Read my post #19, use heap instead of stack, container crashes.

  6. #6
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QVector crashes when array size is big

    Quote Originally Posted by Sheng View Post
    Read my post #19, use heap instead of stack, container crashes.

    How do you use heap? Try this:

    Qt Code:
    1. #include <stdio.h>
    2. #include <unistd.h>
    3.  
    4. struct myStruc {
    5. int *array;
    6. myStruc() {
    7. array = new int[ 3000*2000 ];
    8. }
    9. ~myStruc() {
    10. delete [] array;
    11. }
    12. };
    13.  
    14.  
    15. void foo(const myStruc &v)
    16. {
    17. printf("%d\n", v.array[1]);
    18. }
    19.  
    20. int main(int , char* [])
    21. {
    22. printf("hello\n");
    23. fflush(stdout);
    24. sleep(1);
    25.  
    26. myStruc currentStruc;
    27. foo(currentStruc);
    28. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    Quote Originally Posted by lni View Post
    How do you use heap? Try this:

    Qt Code:
    1. #include <stdio.h>
    2. #include <unistd.h>
    3.  
    4. struct myStruc {
    5. int *array;
    6. myStruc() {
    7. array = new int[ 3000*2000 ];
    8. }
    9. ~myStruc() {
    10. delete [] array;
    11. }
    12. };
    13.  
    14.  
    15. void foo(const myStruc &v)
    16. {
    17. printf("%d\n", v.array[1]);
    18. }
    19.  
    20. int main(int , char* [])
    21. {
    22. printf("hello\n");
    23. fflush(stdout);
    24. sleep(1);
    25.  
    26. myStruc currentStruc;
    27. foo(currentStruc);
    28. }
    To copy to clipboard, switch view to plain text mode 
    It also crashes when you push back structure into vector.
    Here is the code:

    Qt Code:
    1. #include <stdio.h>
    2. #include <unistd.h>
    3. #include <QVector>
    4.  
    5. struct myStruc {
    6. int *array;
    7. myStruc() {
    8. array = new int[ 3000*2000 ];
    9. }
    10. ~myStruc() {
    11. delete [] array;
    12. }
    13. };
    14.  
    15.  
    16. void foo(const myStruc &v)
    17. {
    18. printf("%d\n", v.array[1]);
    19. }
    20.  
    21. int main(int , char* [])
    22. {
    23. printf("hello\n");
    24. fflush(stdout);
    25. sleep(1);
    26.  
    27. myStruc currentStruc;
    28. foo(currentStruc);
    29. QVector<myStruc> myVector;
    30. myVector.push_back(currentStruc);
    31. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QVector crashes when array size is big

    It crashes because you don't have copy constructor. The QVector needs a copy constructor and it uses a default shadow copy constructor. Your "array" pointer is deleted twice when it goes out of scope...

    Run the following program and see how many times the constructor is called and how many times the destructor is called.

    Qt Code:
    1. #include <stdio.h>
    2. #include <unistd.h>
    3. #include <QVector>
    4. #include <QDebug>
    5.  
    6. struct myStruc {
    7. int *array;
    8. myStruc() {
    9. qDebug() << "myStruc";
    10. array = new int[ 3000*2000 ];
    11. }
    12.  
    13. ~myStruc() {
    14. qDebug() << "~myStruc";
    15. delete [] array;
    16. }
    17. };
    18.  
    19.  
    20. void foo(const myStruc &v)
    21. {
    22. printf("%d\n", v.array[1]);
    23. }
    24.  
    25. int main(int , char* [])
    26. {
    27. printf("hello\n");
    28. fflush(stdout);
    29. sleep(1);
    30.  
    31. myStruc currentStruc;
    32. foo(currentStruc);
    33. QVector<myStruc> myVector;
    34. myVector.push_back(currentStruc);
    35. }
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to lni for this useful post:

    Sheng (26th February 2009)

  10. #9
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    OK, thanks, you are right, both of programs below works fine.

    1.
    Qt Code:
    1. #include <stdio.h>
    2. #include <unistd.h>
    3. #include <QVector>
    4. #include <QDebug>
    5.  
    6. struct myStruc {
    7. int *array;
    8. myStruc() {
    9. qDebug() << "myStruc";
    10. array = new int[ 3000*2000 ];
    11. }
    12.  
    13. myStruc(const myStruc& struc1) {
    14. qDebug() << "copyStruc";
    15. array = new int[3000*2000];
    16. for(int i=0;i<3000*2000;++i)
    17. array[i]=struc1.array[i];
    18. }
    19.  
    20. ~myStruc() {
    21. qDebug() << "~myStruc";
    22. delete [] array;
    23. }
    24. };
    25.  
    26.  
    27. void foo(const myStruc &v)
    28. {
    29. printf("%d\n", v.array[1]);
    30. }
    31.  
    32. int main(int , char* [])
    33. {
    34. printf("hello\n");
    35. fflush(stdout);
    36. sleep(1);
    37.  
    38. myStruc currentStruc;
    39. foo(currentStruc);
    40. QVector<myStruc> myVector;
    41. myVector.push_back(currentStruc);
    42. }
    To copy to clipboard, switch view to plain text mode 

    2.

    Qt Code:
    1. int main(int,char* [])
    2. {
    3. QVector<int> array(3000*2000);
    4. QVector<QVector<int> > myVector;
    5. myVector.push_back(array);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Sheng; 26th February 2009 at 20:27.

  11. #10
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QVector crashes when array size is big

    You 2nd example is a no-op for the myStruct. You define a "myStruct" but it is not used in your main example code. QVector<int> array(3000*2000) is allocated in heap by QVector.

    More over, even if you have enough stack size, the code is dangerous because your program could run in other machine which doesn't have enough stack size, it would be a nightmare to debug because it runs in your development machine, and crash in the client machines.

    Avoid excessive use of stack memory, certainly not 2000x3000 size of int, which is 6,000,000 int array!

  12. #11
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    Quote Originally Posted by lni View Post
    You 2nd example is a no-op for the myStruct. You define a "myStruct" but it is not used in your main example code. QVector<int> array(3000*2000) is allocated in heap by QVector.

    More over, even if you have enough stack size, the code is dangerous because your program could run in other machine which doesn't have enough stack size, it would be a nightmare to debug because it runs in your development machine, and crash in the client machines.

    Avoid excessive use of stack memory, certainly not 2000x3000 size of int, which is 6,000,000 int array!
    Why this one crashes? I thought everything is in heap, no?

    Qt Code:
    1. struct myStruc {
    2. QString myString;
    3. int array[3000*2000];
    4. };
    5.  
    6. void foo(const myStruc &v)
    7. {
    8. printf("%d\n", v.array[1]);
    9. }
    10.  
    11. int main(int , char* [])
    12. {
    13. printf("hello\n");
    14. fflush(stdout);
    15. sleep(1);
    16.  
    17. myStruc* currentStruc=new myStruc;
    18. foo(*currentStruc);
    19. QVector<myStruc> myVector;
    20. myVector.push_back(*currentStruc);
    21. }
    To copy to clipboard, switch view to plain text mode 

  13. #12
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QVector crashes when array size is big

    myStruc* currentStruc=new myStruct is in heap, but the element inside myStruct is in stack!

  14. #13
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    Quote Originally Posted by wysota View Post
    6*3000*2000*sizeof(int) = 36M*sizeof(int) = 144 (or 288) MB just for creating the array. Aren't you running out of memory? If not, are you sure C++ copies arrays? I doubt that so copying the struct might lose its integrity.
    the Container will crash even I only push_back one object.

  15. #14
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QVector crashes when array size is big

    QVector probably allocates memory for several objects at a time, to avoid having to reallocate at every push_back(). How much RAM is there in your testbox, Sheng?

    Does it work if you supply an initial vector size, e.g:

    Qt Code:
    1. QVector<myStruc> myVector(5);
    To copy to clipboard, switch view to plain text mode 
    Last edited by drhex; 25th February 2009 at 13:48.

  16. #15
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    Quote Originally Posted by drhex View Post
    QVector probably allocates memory for several objects at a time, to avoid having to reallocate at every push_back(). How much RAM is there in your testbox, Sheng?
    at least 1G.
    It does not work either with reserve or resize.
    Last edited by Sheng; 25th February 2009 at 14:14.

  17. #16
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    Just wrote and tested my own linked list, it works fine.

  18. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QVector crashes when array size is big

    Quote Originally Posted by Sheng View Post
    Just wrote and tested my own linked list, it works fine.
    Which proves exactly nothing.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #18
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    Quote Originally Posted by wysota View Post
    Which proves exactly nothing.
    It at least demonstrated the problem is not from the limitation of the system, but from the container.

  20. #19
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QVector crashes when array size is big

    I've experimented some more and abbreviated the program:

    Qt Code:
    1. #include<QVector>
    2.  
    3. struct myStruc {
    4. int array[3000*2000];
    5. };
    6.  
    7. int main(int , char* [])
    8. {
    9. printf("hello\n");
    10. fflush(stdout);
    11. sleep(1);
    12.  
    13. QVector<myStruc> myVector;
    14. myStruc currentStruc;
    15. myVector.push_back(currentStruc);
    16. }
    To copy to clipboard, switch view to plain text mode 

    For array-sizes upto 3000*232 (=2.8MB), the above program works as expected.

    For array-sizes between 3000*233 and 3000*698, it prints "hello" and then segfaults.

    For array-sizes 3000*699 (=8.4MB) and up, it segfaults immediately, without even printing "hello".

    Adding a QApplication object did not help.

    Tested with gcc 4.3.2 on Ubuntu 8.10 using Qt-4.5.0-beta1.

    Sheng, you've found the struct-of-death!

  21. #20
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    Quote Originally Posted by drhex View Post
    I've experimented some more and abbreviated the program:

    Qt Code:
    1. #include<QVector>
    2.  
    3. struct myStruc {
    4. int array[3000*2000];
    5. };
    6.  
    7. int main(int , char* [])
    8. {
    9. printf("hello\n");
    10. fflush(stdout);
    11. sleep(1);
    12.  
    13. QVector<myStruc> myVector;
    14. myStruc currentStruc;
    15. myVector.push_back(currentStruc);
    16. }
    To copy to clipboard, switch view to plain text mode 

    For array-sizes upto 3000*232 (=2.8MB), the above program works as expected.

    For array-sizes between 3000*233 and 3000*698, it prints "hello" and then segfaults.

    For array-sizes 3000*699 (=8.4MB) and up, it segfaults immediately, without even printing "hello".

    Adding a QApplication object did not help.

    Tested with gcc 4.3.2 on Ubuntu 8.10 using Qt-4.5.0-beta1.

    Sheng, you've found the struct-of-death!
    Nice research, thanks.

Similar Threads

  1. How to find best size for QTableWidget?
    By plamkata in forum Qt Programming
    Replies: 3
    Last Post: 24th July 2008, 19:07
  2. QT-wince and QHttp::readAll() trouble....
    By AcerExtensa in forum Qt for Embedded and Mobile
    Replies: 6
    Last Post: 12th June 2008, 09:40
  3. QLabel size policy
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2007, 17:57
  4. Replies: 1
    Last Post: 24th October 2006, 16:40
  5. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 22:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.