Page 1 of 3 123 LastLast
Results 1 to 20 of 50

Thread: QVector crashes when array size is big

  1. #1
    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 QVector crashes when array size is big

    Qt Code:
    1. #include<QApplication>
    2. #include<iostream>
    3. #include<QString>
    4. #include<QVector>
    5.  
    6. struct myStruc
    7. {
    8. QString name;
    9. int array[3000*2000];
    10. };
    11.  
    12. int main(int argc,char* argv[])
    13. {
    14. QApplication app(argc,argv);
    15. QVector<myStruc> myVector;
    16. myStruc currentStruc;
    17. for (int i=0;i<5;i++)
    18. {
    19. currentStruc.name+="a";
    20. currentStruc.array[0]=i;
    21. myVector.push_back(currentStruc);
    22. }
    23. for(int i=0;i<5;i++)
    24. {
    25. std::cout<<myVector[i].name.toStdString()<<" "<<myVector[i].array[0]<<std::endl;
    26. }
    27. myVector.clear();
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

    Compile fine, got "segmentation fault" error when run it.
    It works OK if I decrease array size to 3000, any solutions?
    Thanks in advance.

  2. #2
    Join Date
    Feb 2009
    Posts
    24
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    Have you stepped through it in a debugger or added qDebug() output to determine where you're getting the seg fault?

  3. #3
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    what happens when u try other container classes like QList?

  4. #4
    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 talk2amulya View Post
    what happens when u try other container classes like QList?
    Same thing happens (QVector,QList,QLinkedList, std::vector, std::list). Did some search and found this looks like a notorious problem of STL container. I guess Qt Container might use STL during implementation since STL Containers crash as well.
    Anyway, I will write a simple list container tomorrow.
    Last edited by Sheng; 24th February 2009 at 21:59.

  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 abernat View Post
    Have you stepped through it in a debugger or added qDebug() output to determine where you're getting the seg fault?
    The segmentation fault comes when push_back function is called.

  6. #6
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    well, the issue is that QVector and other containers allocate CONTIGUOUS memory..and ur every array takes 24+MB of memory plus QString which is also dynamic..so there are definitely chances that when u try to push back one more..it being another contiguous location enters into another segment..thus a segmentation fault..a solution to this would be PROBABLY create a container that doesn't allocate contiguous memory but spreaded..but still its risky cuz amount being allocated is huge...maybe some guru can give a better insight on this

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  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

    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 

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  10. #10
    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.

  11. #11
    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.

  12. #12
    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.

  13. #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

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

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  15. #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 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.

  16. #16
    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!

  17. #17
    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.

  18. #18
    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

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

    The above also crashes without printing hello, and now there isn't any Qt code left! Seems like a compiler or OS problem.
    Last edited by drhex; 25th February 2009 at 21:36.

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

    The above also crashes without printing hello, and now there isn't any Qt code left! Seems like a compiler or OS problem.
    try use heap to see what happens. Should work fine, then try to push back to the container.
    Here is what I got:
    hello
    0
    segmentation fault
    Last edited by Sheng; 25th February 2009 at 21:47.

  20. #20
    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

    Qt Code:
    1. #include <stdio.h>
    2. #include <sys/resource.h>
    3.  
    4. int main(int , char *[])
    5. {
    6. struct rlimit rlim;
    7. getrlimit(RLIMIT_STACK, &rlim);
    8. printf("max stack size=%d bytes\n", (int)rlim.rlim_cur);
    9. }
    To copy to clipboard, switch view to plain text mode 

    This prints

    max stack size=8388608 bytes

    on my machine. One learns something every day!

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.