Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 50

Thread: QVector crashes when array size is big

  1. #21
    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.

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

    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.


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

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

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

  6. #26
    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. #27
    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. #28
    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. #29
    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. #30
    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. #31
    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. #32
    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. #33
    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
    myStruc* currentStruc=new myStruct is in heap, but the element inside myStruct is in stack!
    I do not think so, in my own linked list, I use this and works fine. The maximum stack size is around 8M. the imageData is larger than 10M.

    I defined my structure as follows:
    Qt Code:
    1. struct imageStruc
    2. {
    3. QString fileName;
    4. uint8_t imageData[IMAGE_SIZE];
    5. imageStruc* next;
    6. };
    To copy to clipboard, switch view to plain text mode 

  15. #34
    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 lni View Post
    but the element inside myStruct is in stack!
    No, it's not. If it were on the stack and you returned from the function the frame of which it would be positioned in, the data would get destructed which is of course not the case.

    What is on the stack is probably the local copy of the structure when push_back is called (although I'm not sure as push_back probably takes a const reference and not a copy).

    Sheng: Why do you need that array there in the first place? Can't you use a vector that dynamically allocates its data as it is needed?

    By the way, if you don't need a vector but a linked list, use QLinkedList and not QVector
    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.


  16. #35
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    Quote Originally Posted by lni View Post
    myStruc* currentStruc=new myStruct is in heap, but the element inside myStruct is in stack!
    AFAIK if you allocate a struct on heap ALL its field can only be allocated on heap...

    Now some thoughts that may or may not be relevant :

    • big structures like that should never ever be passed by value anywhere... allocate them on heap and use a QVector<myStruct*> instead of a QVector<myStruct> (or whatever container you may like more)
    • Storing the data directly in the struct instead of a pointer to it is a HUGE design mistake IMO because it forces it to be allocated no matter what and that gets even worse when some copy has to be made. Using a pointer and allocating the data when it is needed would probably be a better idea, would allow sharing and would very probably cause a dramatic decrease of memory usage and a dramatic increase in speed
    • huge contiguous arrays of memory are not a very wise design choice. Maybe you can split that data in smaller pieces? And do you need such an amount of memory anyway (I assume you want several instances of that struct since you are then using containers)?
    Current Qt projects : QCodeEdit, RotiDeCode

  17. #36
    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
    No, it's not. If it were on the stack and you returned from the function the frame of which it would be positioned in, the data would get destructed which is of course not the case.

    What is on the stack is probably the local copy of the structure when push_back is called (although I'm not sure as push_back probably takes a const reference and not a copy).

    Sheng: Why do you need that array there in the first place? Can't you use a vector that dynamically allocates its data as it is needed?

    By the way, if you don't need a vector but a linked list, use QLinkedList and not QVector
    Yes, actually I used linked list, just use QVector for the example since all of them crashes.

  18. #37
    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
    No, it's not. If it were on the stack and you returned from the function the frame of which it would be positioned in, the data would get destructed which is of course not the case.
    QLinkedList and not QVector
    In his example, the stack is contained inside the heap. The stack will only get destructed when the heap is deleted, it will not go out of scope if the heap is no deleted. When you do "new myStruct" to construct the object, where does the array[ 2000x300 ] come from? It comes from the stack, and it will crash immediately at the "new myStruct".

    As I said, the element inside the structure is in the stack, change it to heap and it should work.

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

    Quote Originally Posted by Sheng
    Yes, actually I used linked list, just use QVector for the example since all of them crashes.
    Still you didn't say what is the array meant for.

    Quote Originally Posted by lni View Post
    In his example, the stack is contained inside the heap.
    I'm sorry, but are you sure you know what the stack is? Because based on your post I think you don't.
    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.


  20. #39
    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 fullmetalcoder View Post
    AFAIK if you allocate a struct on heap ALL its field can only be allocated on heap...

    Now some thoughts that may or may not be relevant :

    • big structures like that should never ever be passed by value anywhere... allocate them on heap and use a QVector<myStruct*> instead of a QVector<myStruct> (or whatever container you may like more)
    • Storing the data directly in the struct instead of a pointer to it is a HUGE design mistake IMO because it forces it to be allocated no matter what and that gets even worse when some copy has to be made. Using a pointer and allocating the data when it is needed would probably be a better idea, would allow sharing and would very probably cause a dramatic decrease of memory usage and a dramatic increase in speed
    • huge contiguous arrays of memory are not a very wise design choice. Maybe you can split that data in smaller pieces? And do you need such an amount of memory anyway (I assume you want several instances of that struct since you are then using containers)?
    Copy has to be made since I need to copy the image from the buffer to somewhere else, the buffer is updated quickly.

  21. #40
    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 fullmetalcoder View Post
    AFAIK if you allocate a struct on heap ALL its field can only be allocated on heap...
    FALSE.

    Try this and tell me why it works.

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

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.