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

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

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

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

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

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

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

    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 

  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

    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 

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

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

    Sheng (26th February 2009)

  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

    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.

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

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

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

    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.

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

  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

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

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.