QList<QVector< . >>: how to reserve
-
Hello everybody.
In my application I have to manage several vectors of float numbers and I figured I would organize them in a QList.
Since such vectors may become quite large (several kbytes up to few Mbytes each), I though it would be better to use the reserve() function to avoid memory issues.If, for example, I want N vectors with maximum allowed size M, what is the best way to reserve my memory for it?
For context, I have in my mainwindow.h
QList<QVector<float>*> *myList;
and in mainwindow.cpp
int N = ... int M = ... myList = new QList<QVector<float>*>();
After that, what is the best way to reserve
myList
?myList->clear(); myList->reserve(N); for(int i=0; i<N; i++) { myList->append(new QVector<float>()); myList->at(i)->reserve(M); }
or should I
myList->reserve(N*M);
?
(consider that M may be several thousends)Are there better ways to do it that I didn't consider?
-
Hello everybody.
In my application I have to manage several vectors of float numbers and I figured I would organize them in a QList.
Since such vectors may become quite large (several kbytes up to few Mbytes each), I though it would be better to use the reserve() function to avoid memory issues.If, for example, I want N vectors with maximum allowed size M, what is the best way to reserve my memory for it?
For context, I have in my mainwindow.h
QList<QVector<float>*> *myList;
and in mainwindow.cpp
int N = ... int M = ... myList = new QList<QVector<float>*>();
After that, what is the best way to reserve
myList
?myList->clear(); myList->reserve(N); for(int i=0; i<N; i++) { myList->append(new QVector<float>()); myList->at(i)->reserve(M); }
or should I
myList->reserve(N*M);
?
(consider that M may be several thousends)Are there better ways to do it that I didn't consider?
@Davide87 said in QList<QVector< . >>: how to reserve:
QList<QVector<float>*> *myList;
Why a pointer here? Unneeded
or should I myList->reserve(N*M);?
It depends on how many items you want to insert into the subvectors - from your descriptions they contain max 'M' elements so why would you reserve N*M then?
-
Hello everybody.
In my application I have to manage several vectors of float numbers and I figured I would organize them in a QList.
Since such vectors may become quite large (several kbytes up to few Mbytes each), I though it would be better to use the reserve() function to avoid memory issues.If, for example, I want N vectors with maximum allowed size M, what is the best way to reserve my memory for it?
For context, I have in my mainwindow.h
QList<QVector<float>*> *myList;
and in mainwindow.cpp
int N = ... int M = ... myList = new QList<QVector<float>*>();
After that, what is the best way to reserve
myList
?myList->clear(); myList->reserve(N); for(int i=0; i<N; i++) { myList->append(new QVector<float>()); myList->at(i)->reserve(M); }
or should I
myList->reserve(N*M);
?
(consider that M may be several thousends)Are there better ways to do it that I didn't consider?
@Davide87 said in QList<QVector< . >>: how to reserve:
or should I
myList->reserve(N*M);
?No, the
QList
andQVector
are quite separate from one another, not related. Any reservations must be done on each one separately, and for the elements of the list which are vectors in each vector individually, as your code does.myList->at(i)->reserve(M);
Not sure you will be able to do this (or have you already done it and it compiles)? const T &QList::at(int i) const returns a
const
reference to the (QVector
) element, so you won't be able to callQList::reserve()
on it? Create a temporary for thenew QVector<float>()
, callreserve()
on that, then add it to yourQList
. -
@Davide87 said in QList<QVector< . >>: how to reserve:
or should I
myList->reserve(N*M);
?No, the
QList
andQVector
are quite separate from one another, not related. Any reservations must be done on each one separately, and for the elements of the list which are vectors in each vector individually, as your code does.myList->at(i)->reserve(M);
Not sure you will be able to do this (or have you already done it and it compiles)? const T &QList::at(int i) const returns a
const
reference to the (QVector
) element, so you won't be able to callQList::reserve()
on it? Create a temporary for thenew QVector<float>()
, callreserve()
on that, then add it to yourQList
.@JonB said in QList<QVector< . >>: how to reserve:
Create a temporary for the new QVector<float>(), call reserve() on that, then add it to your QList.
or use
operator[]
:) -
@JonB said in QList<QVector< . >>: how to reserve:
Create a temporary for the new QVector<float>(), call reserve() on that, then add it to your QList.
or use
operator[]
:)@Christian-Ehrlicher
I could have suggested that, but to me I should not have to think about how I am addressing the element in the list ([]
vsat()
) when all I want to do is setreserve()
on theQVector
I am adding. I also have a feeling it will be 1 nanosecond slower to do it via[]
/on an element I have already put into the list than if I do it do it beforehand (though i amy be wrong on this)... ;-)If you can afford to actually create all the elements (value
0.0
) initially and overwrite them instead ofreserve()
ing (because it will be fixed size), Qt6 at least seems to have QList::QList(qsizetype size) constructor which you can use. -
Thank you guys.
@Christian-Ehrlicher
In my application N is in the order of few tens. The subvectors may be instead hundreds or thousands of elements, depending on the configuration of the hardware that sends me the data to store/display.
I thought I may have to reserve the list depending on the actual maximum number of bytes it is going to contain.
The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.@JonB
myList->at(i)->reserve(M); compiles without issues.
I'm actually having some memory issues in my application, but they are probably related to other parts of may code. That's why I opened this topic. -
Thank you guys.
@Christian-Ehrlicher
In my application N is in the order of few tens. The subvectors may be instead hundreds or thousands of elements, depending on the configuration of the hardware that sends me the data to store/display.
I thought I may have to reserve the list depending on the actual maximum number of bytes it is going to contain.
The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.@JonB
myList->at(i)->reserve(M); compiles without issues.
I'm actually having some memory issues in my application, but they are probably related to other parts of may code. That's why I opened this topic.@Davide87 said in QList<QVector< . >>: how to reserve:
@JonB
myList->at(i)->reserve(M); compiles without issues.@Christian-Ehrlicher could you explain, am I having a mind block? Given it's const T &QList::at(int i) const, doesn't the
const T &
prevent me from callingQList::reserve()
on it?@Davide87
I still suspectreserve()
won't make that much difference, Qt's list/vector allocation will be reasonable even without it. You would have to test.The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.
I don't think
reserve()
will have any effect on the maximum size of the list/vector. That will happen dynamically whether you reserve or not. It's only to do with efficient use of memory during re-allocations as list grows. -
@Davide87 said in QList<QVector< . >>: how to reserve:
@JonB
myList->at(i)->reserve(M); compiles without issues.@Christian-Ehrlicher could you explain, am I having a mind block? Given it's const T &QList::at(int i) const, doesn't the
const T &
prevent me from callingQList::reserve()
on it?@Davide87
I still suspectreserve()
won't make that much difference, Qt's list/vector allocation will be reasonable even without it. You would have to test.The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.
I don't think
reserve()
will have any effect on the maximum size of the list/vector. That will happen dynamically whether you reserve or not. It's only to do with efficient use of memory during re-allocations as list grows.@JonB said in QList<QVector< . >>: how to reserve:
Given it's const T &QList::at(int i) const, doesn't the const T & prevent me from calling QList::reserve() on it?
Because he is working with heap-allocated QList/QVector instances for unknown reaons / he wants to debug memory leaks later on because he forgets to delete them afterwards...
-
@Davide87 said in QList<QVector< . >>: how to reserve:
@JonB
myList->at(i)->reserve(M); compiles without issues.@Christian-Ehrlicher could you explain, am I having a mind block? Given it's const T &QList::at(int i) const, doesn't the
const T &
prevent me from callingQList::reserve()
on it?@Davide87
I still suspectreserve()
won't make that much difference, Qt's list/vector allocation will be reasonable even without it. You would have to test.The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.
I don't think
reserve()
will have any effect on the maximum size of the list/vector. That will happen dynamically whether you reserve or not. It's only to do with efficient use of memory during re-allocations as list grows.@JonB said in QList<QVector< . >>: how to reserve:
@Davide87 said in QList<QVector< . >>: how to reserve:
@JonB
myList->at(i)->reserve(M); compiles without issues.@Christian-Ehrlicher could you explain, am I having a mind block? Given it's const T &QList::at(int i) const, doesn't the
const T &
prevent me from callingQList::reserve()
on it?It may be because myList is a list of pointers. QList::at(int i) returns a pointer in this case. Therefore ->reserve() works in this case. Maybe.
@Davide87
I still suspectreserve()
won't make that much difference, Qt's list/vector allocation will be reasonable even without it. You would have to test.The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.
I don't think
reserve()
will have any effect on the maximum size of the list/vector. That will happen dynamically whether you reserve or not. It's only to do with efficient use of memory during re-allocations as list grows.Ok, thank you. I will test that.
-
@JonB said in QList<QVector< . >>: how to reserve:
@Davide87 said in QList<QVector< . >>: how to reserve:
@JonB
myList->at(i)->reserve(M); compiles without issues.@Christian-Ehrlicher could you explain, am I having a mind block? Given it's const T &QList::at(int i) const, doesn't the
const T &
prevent me from callingQList::reserve()
on it?It may be because myList is a list of pointers. QList::at(int i) returns a pointer in this case. Therefore ->reserve() works in this case. Maybe.
@Davide87
I still suspectreserve()
won't make that much difference, Qt's list/vector allocation will be reasonable even without it. You would have to test.The size of the vectors can vary, but I would prefer to set a maximum size to avoid my application to take too much memory.
I don't think
reserve()
will have any effect on the maximum size of the list/vector. That will happen dynamically whether you reserve or not. It's only to do with efficient use of memory during re-allocations as list grows.Ok, thank you. I will test that.
@Davide87 said in QList<QVector< . >>: how to reserve:
It may be because myList is a list of pointers. QList::at(int i) returns a pointer in this case. Therefore ->reserve() works in this case. Maybe.
Ah, yes indeed, I did not notice that. Only the pointer is
const
then, it does not make*const_pointer
beconst
:) -
Again: don't use pointers here - they are completely unneeded and will get you in trouble later on when you hunt for memory leaks
-
-
@Davide87 said in QList<QVector< . >>: how to reserve:
or should I
myList->reserve(N*M);
?No, the
QList
andQVector
are quite separate from one another, not related. Any reservations must be done on each one separately, and for the elements of the list which are vectors in each vector individually, as your code does.myList->at(i)->reserve(M);
Not sure you will be able to do this (or have you already done it and it compiles)? const T &QList::at(int i) const returns a
const
reference to the (QVector
) element, so you won't be able to callQList::reserve()
on it? Create a temporary for thenew QVector<float>()
, callreserve()
on that, then add it to yourQList
.@JonB said in QList<QVector< . >>: how to reserve:
No, the QList and QVector are quite separate from one another, not related
Not in Qt6
https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qvector.h
you're technically correct in relation to the OP post. And technically correct is the best kind of correct
-
@JonB said in QList<QVector< . >>: how to reserve:
No, the QList and QVector are quite separate from one another, not related
Not in Qt6
https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qvector.h
you're technically correct in relation to the OP post. And technically correct is the best kind of correct
@J-Hilk
I did not sayQList
andQVector
are not related to one another! I said THEQList
and THEQVector
in OP'sQList<QVector<float>*> *myList;
declaration are unrelated to each other (Qt4, 5, 6 or anything else). The OP asked whether
or should I
myList->reserve(N*M);
?where he thinks a list/vector of lists/vectors are "one blob" which can be reserved like that....
-
@J-Hilk
I did not sayQList
andQVector
are not related to one another! I said THEQList
and THEQVector
in OP'sQList<QVector<float>*> *myList;
declaration are unrelated to each other (Qt4, 5, 6 or anything else). The OP asked whether
or should I
myList->reserve(N*M);
?where he thinks a list/vector of lists/vectors are "one blob" which can be reserved like that....
@J-Hilk said in QList<QVector< . >>: how to reserve:
you're technically correct in relation to the OP post. And technically correct is the best kind of correct