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!
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:
struct imageStruc { QString fileName; uint8_t imageData[IMAGE_SIZE]; imageStruc* next; };To copy to clipboard, switch view to plain text mode
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
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
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.
FALSE.
Try this and tell me why it works.
Qt Code:
#include <QVector> #include <QString> struct myStruc { QString myString; int *array; myStruc() { array = new int[ 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
Bookmarks