Page 3 of 3 FirstFirst 123
Results 41 to 50 of 50

Thread: QVector crashes when array size is big

  1. #41
    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
    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.
    no, it's not, and it works in my code. the crash comes not from "new myStruct", but from push_back.

  2. #42
    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
    no, it's not, and it works in my code. the crash comes not from "new myStruct", but from push_back.
    Your computer may have enough resource to hold 1 copy of array[ 2000 x 3000 ], and crash if you have two copies, that is precisely what QVector does, it copies your structure when you do push_back the object (value based object).

    Go back review my post on calling the destructor and copy constructor prinout, and how you add a copy constructor to fix the crash.

  3. #43
    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
    FALSE.

    Try this and tell me why it works.
    :-)

    Explain this:

    Qt Code:
    1. #include "stdio.h"
    2. #include <QString>
    3.  
    4. struct A {
    5. int array[1000];
    6. };
    7.  
    8. int main(){
    9. A a;
    10. A *b = new A;
    11. printf("Addr of a: 0x%08x\n", (unsigned int)&a);
    12. printf("Addr of a.array: 0x%08x\n", (unsigned int)a.array);
    13. printf("Addr of b: 0x%08x\n", (unsigned int)b);
    14. printf("Addr of b->array: 0x%08x\n", (unsigned int)b->array);
    15. return 0;
    16. }
    To copy to clipboard, switch view to plain text mode 

    Result:
    txt Code:
    1. Addr of a: 0xbfa5008c
    2. Addr of a.array: 0xbfa50090
    3. Addr of b: 0x089fe1b8
    4. Addr of b->array: 0x089fe1bc
    To copy to clipboard, switch view to plain text mode 

    As you see a and a.array are on the stack and b and b->array are on heap.

    Your "example" works because even if "myStruc" is allocated on the stack, its array member will be allocated on the heap. But it doesn't mean that if myStruc is allocated on the heap, array is (or can be) allocated 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. #44
    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

    By the way, push_back is not a problem. This crashes on the QVector constructor and I think I know why

    Qt Code:
    1. #include <QVector>
    2. #include <QString>
    3.  
    4. struct StructOfDeath {
    5. QString some;
    6. QString innocent;
    7. int variables;
    8. double And;
    9. int memberOfDeath[3000*2000];
    10. StructOfDeath(){ variables = 0; And = 1;}
    11. };
    12.  
    13. int main(){
    14.  
    15. QVector<StructOfDeath> vector;
    16. vector.reserve(30);
    17. for(int i=0;i<10;i++)
    18. vector.push_back(StructOfDeath());
    19. printf("Done\n");
    20. return 0;
    21. }
    To copy to clipboard, switch view to plain text mode 
    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.


  5. #45
    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
    :-)

    As you see a and a.array are on the stack and b and b->array are on heap.

    Your "example" works because even if "myStruc" is allocated on the stack, its array member will be allocated on the heap. But it doesn't mean that if myStruc is allocated on the heap, array is (or can be) allocated on the stack
    Do some google search on "array in stack overflow", or at
    http://www.devx.com/tips/Tip/14276

  6. #46
    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
    Your computer may have enough resource to hold 1 copy of array[ 2000 x 3000 ], and crash if you have two copies, that is precisely what QVector does, it copies your structure when you do push_back the object (value based object).

    Go back review my post on calling the destructor and copy constructor prinout, and how you add a copy constructor to fix the crash.
    There are more than 10 images in the memory, not just two.

  7. #47
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QVector crashes when array size is big

    Quote Originally Posted by lni View Post
    Do some google search on "array in stack overflow", or at
    http://www.devx.com/tips/Tip/14276
    I can't see any connection between that article and things you are saying :P
    As for me it's simple:

    If there's code like this:
    Qt Code:
    1. A a;
    To copy to clipboard, switch view to plain text mode 
    object a of type A is created on stack. If that code appears as a local variable in some function/method then at the end of the function/method object a will be deleted from stack, because program is returning to the caller, to place where call appeared.
    (that's why there's stack overflow in infinite recursion -> infinite storing the called method variables and no returns, which will remove these variables from stack)

    But if there's code like this:
    Qt Code:
    1. A *a = new A;
    To copy to clipboard, switch view to plain text mode 
    instead of previously shown, then it's different story :] it means that your local variable created on a stack is a pointer (32-bit address = 4B) which points to the object of type A created on heap - not afecting stack in any way.

    So if there is an array in struct created dynamically on a heap:
    Qt Code:
    1. struct myStruc {
    2. QString myString;
    3. int *array;
    4. myStruc() {
    5. array = new int[ 3000*2000 ];
    6. }
    7.  
    8. };
    To copy to clipboard, switch view to plain text mode 
    then this struct size is just a size of myString and size of pointer to an array. An array itself is always on heap (beacause of operator new).


    If there is any mistake in my understanding of stack/heap, feel free to provide any interesting links on this topic.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  8. #48
    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
    Do some google search on "array in stack overflow", or at
    http://www.devx.com/tips/Tip/14276
    Honestly I don't see any connection between the issue of the array being on the stack or not and the article. I think the example I gave you is a proof that can't be undermined.

    Stack addresses have high values because in most (all?) modern architectures the stack expands "downwards" (to lower memory addresses) so there has to be enough space for the stack to expand until it reaches the heap space (that is (usually?) between the global variable address space and the stack address space). The example clearly shows that a.array is positioned 4 bytes away from the beginning of the structure in the same adress region so it has to be inside the struct as sizeof(A) will return a value larger than 4 bytes so a logical conclusion follows that both "objects" have to be positioned in the same "part" of memory. The same goes for the second variant, the difference is also 4 bytes and the size of the structure remains the same.

    Here is a result of the extended version of my previous example. Note the difference in structure sizes and addresses.

    txt Code:
    1. Addr of a: 0xbfcaebb0
    2. Addr of a.array: 0xbfcaebb4
    3. Addr of b: 0x0808d1b8
    4. Addr of b->array: 0x0808d1bc
    5. Size of struct A: 4004
    6.  
    7. Addr of c: 0xbfcafb54
    8. Addr of c.array: 0x0808e168
    9. Addr of d: 0x0808f110
    10. Addr of d->array: 0x0808f120
    11. Size of struct B: 8
    To copy to clipboard, switch view to plain text mode 

    And the code itself...

    Qt Code:
    1. #include "stdio.h"
    2. #include <QString>
    3.  
    4. struct A {
    5. int array[1000];
    6. };
    7.  
    8. struct B {
    9. int *array;
    10. B(){ array = new int[1000]; }
    11. };
    12.  
    13. int main(){
    14. A a;
    15. A *b = new A;
    16. B c;
    17. B *d = new B;
    18. printf("Addr of a: 0x%08x\n", (unsigned int)&a);
    19. printf("Addr of a.array: 0x%08x\n", (unsigned int)a.array);
    20. printf("Addr of b: 0x%08x\n", (unsigned int)b);
    21. printf("Addr of b->array: 0x%08x\n", (unsigned int)b->array);
    22. printf("Size of struct A: %d\n", sizeof(A));
    23. printf("\n");
    24. printf("Addr of c: 0x%08x\n", (unsigned int)&c);
    25. printf("Addr of c.array: 0x%08x\n", (unsigned int)c.array);
    26. printf("Addr of d: 0x%08x\n", (unsigned int)d);
    27. printf("Addr of d->array: 0x%08x\n", (unsigned int)d->array);
    28. printf("Size of struct B: %d\n", sizeof(B));
    29.  
    30. return 0;
    31. }
    To copy to clipboard, switch view to plain text mode 
    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.


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

    Thank you all for your posts. I think I am clear of everything about Qt containers.
    I also think every one learn something new, if not, you are good enough.

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

    Sheng, a side question - why don't you use QImage or QByteArray instead of that huge array of yours?
    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.


Similar Threads

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