1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \class QVarLengthArray
|
---|
30 | \brief The QVarLengthArray class provides a low-level variable-length array.
|
---|
31 |
|
---|
32 | \ingroup tools
|
---|
33 | \reentrant
|
---|
34 |
|
---|
35 | The C++ language doesn't support variable-length arrays on the stack.
|
---|
36 | For example, the following code won't compile:
|
---|
37 |
|
---|
38 | \snippet doc/src/snippets/code/doc_src_qvarlengtharray.qdoc 0
|
---|
39 |
|
---|
40 | The alternative is to allocate the array on the heap (with
|
---|
41 | \c{new}):
|
---|
42 |
|
---|
43 | \snippet doc/src/snippets/code/doc_src_qvarlengtharray.qdoc 1
|
---|
44 |
|
---|
45 | However, if myfunc() is called very frequently from the
|
---|
46 | application's inner loop, heap allocation can be a major source
|
---|
47 | of slowdown.
|
---|
48 |
|
---|
49 | QVarLengthArray is an attempt to work around this gap in the C++
|
---|
50 | language. It allocates a certain number of elements on the stack,
|
---|
51 | and if you resize the array to a larger size, it automatically
|
---|
52 | uses the heap instead. Stack allocation has the advantage that
|
---|
53 | it is much faster than heap allocation.
|
---|
54 |
|
---|
55 | Example:
|
---|
56 | \snippet doc/src/snippets/code/doc_src_qvarlengtharray.qdoc 2
|
---|
57 |
|
---|
58 | In the example above, QVarLengthArray will preallocate 1024
|
---|
59 | elements on the stack and use them unless \c{n + 1} is greater
|
---|
60 | than 1024. If you omit the second template argument,
|
---|
61 | QVarLengthArray's default of 256 is used.
|
---|
62 |
|
---|
63 | QVarLengthArray's value type must be an \l{assignable data type}.
|
---|
64 | This covers most data types that are commonly used, but the
|
---|
65 | compiler won't let you, for example, store a QWidget as a value;
|
---|
66 | instead, store a QWidget *.
|
---|
67 |
|
---|
68 | QVarLengthArray, like QVector, provides a resizable array data
|
---|
69 | structure. The main differences between the two classes are:
|
---|
70 |
|
---|
71 | \list
|
---|
72 | \o QVarLengthArray's API is much more low-level. It provides no
|
---|
73 | iterators and lacks much of QVector's functionality.
|
---|
74 |
|
---|
75 | \o QVarLengthArray doesn't initialize the memory if the value is
|
---|
|
---|