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

    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.


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

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

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

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

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

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

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

    Quote Originally Posted by Sheng View Post
    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
    Try using a pointer or a QVector in myStruc instead of that huge int array.

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

    Or a type shorter than int.
    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
    Or a type shorter than int.
    Yes, actually I used uint8_t in my code.

    Quote Originally Posted by drhex View Post
    Try using a pointer or a QVector in myStruc instead of that huge int array.
    I can not use pointer since I need to save image data in the queue.

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.