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.
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 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:
struct myStruc { QString name; int *array; myStruc() : array( new int[ 3000*2000] ) { } // or with size argument in constructor myStruct( size_t sz ) : array( new int[ sz ] ) { } ~myStruc() { delete [] array; } };To copy to clipboard, switch view to plain text mode
Actually QVector allocates its data on the heap even if the vector object itself is on the stack.
How do you use heap? Try this:
Qt Code:
#include <stdio.h> #include <unistd.h> struct myStruc { int *array; myStruc() { array = new int[ 3000*2000 ]; } ~myStruc() { delete [] array; } }; void foo(const myStruc &v) { printf("%d\n", v.array[1]); } int main(int , char* []) { printf("hello\n"); fflush(stdout); sleep(1); myStruc currentStruc; foo(currentStruc); }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:
#include <stdio.h> #include <unistd.h> #include <QVector> struct myStruc { int *array; myStruc() { array = new int[ 3000*2000 ]; } ~myStruc() { delete [] array; } }; void foo(const myStruc &v) { printf("%d\n", v.array[1]); } int main(int , char* []) { printf("hello\n"); fflush(stdout); sleep(1); myStruc currentStruc; foo(currentStruc); QVector<myStruc> myVector; myVector.push_back(currentStruc); }To copy to clipboard, switch view to plain text mode
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:
#include <stdio.h> #include <unistd.h> #include <QVector> #include <QDebug> struct myStruc { int *array; myStruc() { qDebug() << "myStruc"; array = new int[ 3000*2000 ]; } ~myStruc() { qDebug() << "~myStruc"; delete [] array; } }; void foo(const myStruc &v) { printf("%d\n", v.array[1]); } int main(int , char* []) { printf("hello\n"); fflush(stdout); sleep(1); myStruc currentStruc; foo(currentStruc); QVector<myStruc> myVector; myVector.push_back(currentStruc); }To copy to clipboard, switch view to plain text mode
Sheng (26th February 2009)
OK, thanks, you are right, both of programs below works fine.
1.
Qt Code:
#include <stdio.h> #include <unistd.h> #include <QVector> #include <QDebug> struct myStruc { int *array; myStruc() { qDebug() << "myStruc"; array = new int[ 3000*2000 ]; } myStruc(const myStruc& struc1) { qDebug() << "copyStruc"; array = new int[3000*2000]; for(int i=0;i<3000*2000;++i) array[i]=struc1.array[i]; } ~myStruc() { qDebug() << "~myStruc"; delete [] array; } }; void foo(const myStruc &v) { printf("%d\n", v.array[1]); } int main(int , char* []) { printf("hello\n"); fflush(stdout); sleep(1); myStruc currentStruc; foo(currentStruc); QVector<myStruc> myVector; myVector.push_back(currentStruc); }To copy to clipboard, switch view to plain text mode
2.
Qt Code:
int main(int,char* []) { QVector<int> array(3000*2000); QVector<QVector<int> > myVector; myVector.push_back(array); }To copy to clipboard, switch view to plain text mode
Last edited by Sheng; 26th February 2009 at 20:27.
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:
struct myStruc { QString myString; int array[3000*2000]; }; void foo(const myStruc &v) { printf("%d\n", v.array[1]); } int main(int , char* []) { printf("hello\n"); fflush(stdout); sleep(1); myStruc* currentStruc=new myStruc; foo(*currentStruc); QVector<myStruc> myVector; myVector.push_back(*currentStruc); }To copy to clipboard, switch view to plain text mode
myStruc* currentStruc=new myStruct is in heap, but the element inside myStruct is in stack!
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:
QVector<myStruc> myVector(5);To copy to clipboard, switch view to plain text mode
Last edited by drhex; 25th February 2009 at 13:48.
Just wrote and tested my own linked list, it works fine.
I've experimented some more and abbreviated the program:
Qt Code:
#include<QVector> struct myStruc { int array[3000*2000]; }; int main(int , char* []) { printf("hello\n"); fflush(stdout); sleep(1); QVector<myStruc> myVector; myStruc currentStruc; myVector.push_back(currentStruc); }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!![]()
Bookmarks