我试了下,我的电脑可以放60M左右的数据。栈内存是1~2M,堆更大,但vector是在哪分配的内存呢?
------解决方案--------------------
应该是堆内存。我的电脑上QVector<__int64>可以放67100000个数据,如果放67200000则失败。
------解决方案--------------------
struct Q_CORE_EXPORT QVectorData
{
QBasicAtomicInt ref;
int alloc;
int size;
#if defined(QT_ARCH_SPARC) && defined(Q_CC_GNU) && defined(__LP64__) && defined(QT_BOOTSTRAPPED)
// workaround for bug in gcc 3.4.2
uint sharable;
uint capacity;
uint reserved;
#else
uint sharable : 1;
uint capacity : 1;
uint reserved : 30;
#endif
static QVectorData shared_null;
// ### Qt 5: rename to 'allocate()'. The current name causes problems for
// some debugges when the QVector is member of a class within an unnamed namespace.
// ### Qt 5: can be removed completely. (Ralf)
static QVectorData *malloc(int sizeofTypedData, int size, int sizeofT, QVectorData *init);
static QVectorData *allocate(int size, int alignment);
static QVectorData *reallocate(QVectorData *old, int newsize, int oldsize, int alignment);
static void free(QVectorData *data, int alignment);
static int grow(int sizeofTypedData, int size, int sizeofT, bool excessive);
};
看这段代码,你就可以看出,QVector是分配在heap上的。所有容器类一般都是分配在堆上的