QMemArray(3qt)
๐ QMemArray ๐
okay bestie, this is the manual page for QMemArray(3qt) โ she's a template class that provides arrays of simple types, and yes she's been here since forever, and yes she still slays โจ
โจ NAME โจ
QMemArray โ Template class that provides arrays of simple types
๐ฆ SYNOPSIS ๐ฆ
Okay so first things first: all the functions in this class are reentrant when Qt is built with thread support. </p> (yes that stray tag is literally in the original, we don't gatekeep ๐)
#include <qmemarray.h>
Inherited by QByteArray and QPointArray. Passing the genes down fr fr ๐
๐ธ Public Members ๐ธ
typedef type * Iteratortypedef const type * ConstIteratorQMemArray ()QMemArray ( int size )QMemArray ( const QMemArray<type> & a )~QMemArray ()QMemArray<type> & operator= ( const QMemArray<type> & a )type * data () constuint nrefs () constuint size () constuint count () constbool isEmpty () constbool isNull () constbool resize ( uint size )bool resize ( uint size, Optimization optim )bool truncate ( uint pos )bool fill ( const type & v, int size = -1 )virtual void detach ()QMemArray<type> copy () constQMemArray<type> & assign ( const QMemArray<type> & a )QMemArray<type> & assign ( const type * data, uint size )QMemArray<type> & duplicate ( const QMemArray<type> & a )QMemArray<type> & duplicate ( const type * data, uint size )QMemArray<type> & setRawData ( const type * data, uint size )void resetRawData ( const type * data, uint size )int find ( const type & v, uint index = 0 ) constint contains ( const type & v ) constvoid sort ()int bsearch ( const type & v ) consttype & operator[] ( int index ) consttype & at ( uint index ) constoperator const type * () constbool operator== ( const QMemArray<type> & a ) constbool operator!= ( const QMemArray<type> & a ) constIterator begin ()Iterator end ()ConstIterator begin () constConstIterator end () const
๐ Protected Members ๐
QMemArray ( int, int )
๐ RELATED FUNCTION DOCUMENTATION ๐
Q_UINT16 qChecksum ( const char * data, uint len )QDataStream & operator<< ( QDataStream & s, const QByteArray & a )QDataStream & operator>> ( QDataStream & s, QByteArray & a )
๐ DESCRIPTION ๐
The QMemArray class is a template class that provides arrays of simple types. That's the whole vibe. Simple types only, no drama ๐
QMemArray is implemented as a template class. Define a template instance QMemArray<X> to create an array that contains X items.
Now, the tea: QMemArray stores the array elements directly in the array. She can only deal with simple types โ like, C++ types, structs, and classes that have no constructors, no destructors, and no virtual functions. QMemArray uses bitwise operations to copy and compare array elements. Bitwise. She's not reading your constructors, she's just copying bytes, okay? ๐ญ
The QPtrVector collection class is also a kind of array. Like most collection classes, it uses pointers to the contained items. Different energy, still valid ๐
QMemArray uses explicit sharing with a reference count. And bestie, listen โ if more than one array shares common data and one of the arrays is modified, all the arrays are modified. That's not a bug, that's the design, and it will humble you if you forget it ๐
The benefit of sharing is that a program does not need to duplicate data when it is not required, which results in lower memory use and less copying of data. So like, efficient. Iconic. Lowkey genius โจ
An alternative to QMemArray is QValueVector. The QValueVector class also provides an array of objects, but can deal with objects that have constructors (specifically a copy constructor and a default constructor). QValueVector provides an STL-compatible syntax and is implicitly shared. So if you need actual constructors, that's your girl ๐ธ
Example:
#include <qmemarray.h>
#include <stdio.h>
QMemArray<int> fib( int num ) // returns fibonacci array
{
Q_ASSERT( num > 2 );
QMemArray<int> f( num ); // array of ints
f[0] = f[1] = 1;
for ( int i = 2; i < num; i++ )
f[i] = f[i-1] + f[i-2];
return f;
}
int main()
{
QMemArray<int> a = fib( 6 ); // get first 6 fibonaccis
for ( int i = 0; i < a.size(); i++ )
qDebug( "%d: %d", i, a[i] );
qDebug( "1 is found %d times", a.contains(1) );
qDebug( "5 is found at index %d", a.find(5) );
return 0;
}
Program output:
0: 1
1: 1
2: 2
3: 3
4: 5
5: 8
1 is found 2 times
5 is found at index 4
Okay now the important warning, no cap. Note concerning the use of QMemArray for manipulating structs or classes: Compilers will often pad the size of structs of odd sizes up to the nearest word boundary. This will then be the size QMemArray will use for its bitwise element comparisons. Because the remaining bytes will typically be uninitialized, this can cause find() etc. to fail to find the element. Example:
// MyStruct may be padded to 4 or 8 bytes
struct MyStruct
{
short i; // 2 bytes
char c; // 1 byte
};
QMemArray<MyStruct> a(1);
a[0].i = 5;
a[0].c = 't';
MyStruct x;
x.i = '5';
x.c = 't';
int i = a.find( x ); // may return -1 if the pad bytes differ
To work around this, make sure that you use a struct where sizeof() returns the same as the sum of the sizes of the members either by changing the types of the struct members or by adding dummy members. Dummy members are literally the padding's therapist ๐ซถ
QMemArray data can be traversed by iterators (see begin() and end()). The number of items is returned by count(). The array can be resized with resize() and filled using fill().
You can make a shallow copy of the array with assign() (or operator=()) and a deep copy with duplicate().
Search for values in the array with find() and contains(). For sorted arrays (see sort()) you can search using bsearch().
You can set the data directly using setRawData() and resetRawData(), although this requires care. Like, care care. Not vibes-only care. Actual care ๐ญ
See also Shared Classes and Non-GUI Classes.
๐ง Member Type Documentation ๐ง
QMemArray::ConstIterator
A const QMemArray iterator.
See also begin() and end().
QMemArray::Iterator
A QMemArray iterator.
See also begin() and end().
๐ MEMBER FUNCTION DOCUMENTATION ๐
QMemArray::QMemArray ( int, int ) [protected]
Constructs an array without allocating array space. The arguments should be (0, 0). Use at your own risk. That's it. That's the documentation. She's telling you to be careful and she means it ๐
QMemArray::QMemArray ()
Constructs a null array.
See also isNull().
QMemArray::QMemArray ( int size )
Constructs an array with room for size elements. Makes a null array if size == 0.
The elements are left uninitialized. So don't come crying when they're garbage, bestie, that's the deal โจ
See also resize() and isNull().
QMemArray::QMemArray ( const QMemArray<type> & a )
Constructs a shallow copy of a.
See also assign().
QMemArray::~QMemArray ()
Dereferences the array data and deletes it if this was the last reference. Last one out, turns off the lights ๐
QMemArray<type> & QMemArray::assign ( const QMemArray<type> & a )
Shallow copy. Dereferences the current array and references the data contained in a instead. Returns a reference to this array.
See also operator=().
QMemArray<type> & QMemArray::assign ( const type * data, uint size )
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Shallow copy. Dereferences the current array and references the array data data, which contains size elements. Returns a reference to this array.
Do not deletedatalater;QMemArraywill callfree()on it at the right time.
type & QMemArray::at ( uint index ) const
Returns a reference to the element at position index in the array.
This can be used to both read and set an element. Two-for-one, we love a multitasker ๐
See also operator[]().
Iterator QMemArray::begin ()
Returns an iterator pointing at the beginning of this array. This iterator can be used in the same way as the iterators of QValueList and QMap, for example.
ConstIterator QMemArray::begin () const
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns a const iterator pointing at the beginning of this array. This iterator can be used in the same way as the iterators of QValueList and QMap, for example.
int QMemArray::bsearch ( const type & v ) const
In a sorted array (as sorted by sort()), finds the first occurrence of v by using a binary search. For a sorted array this is generally much faster than find(), which does a linear search.
Returns the position of v, or -1 if v could not be found.
See also sort() and find().
int QMemArray::contains ( const type & v ) const
Returns the number of times v occurs in the array.
See also find().
QMemArray<type> QMemArray::copy () const
Returns a deep copy of this array.
See also detach() and duplicate().
uint QMemArray::count () const
Returns the same as size().
See also size().
Example: scribble/scribble.cpp.
type * QMemArray::data () const
Returns a pointer to the actual array data.
The array is a null array if data() == 0 (null pointer).
See also isNull().
Examples:
void QMemArray::detach () [virtual]
Detaches this array from shared array data; i.e. it makes a private, deep copy of the data.
Copying will be performed only if the reference count is greater than one.
See also copy().
Reimplemented in QBitArray.
QMemArray<type> & QMemArray::duplicate ( const QMemArray<type> & a )
Deep copy. Dereferences the current array and obtains a copy of the data contained in a instead. Returns a reference to this array.
See also copy().
QMemArray<type> & QMemArray::duplicate ( const type * data, uint size )
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Deep copy. Dereferences the current array and obtains a copy of the array data data instead. Returns a reference to this array. The size of the array is given by size.
See also copy().
Iterator QMemArray::end ()
Returns an iterator pointing behind the last element of this array. This iterator can be used in the same way as the iterators of QValueList and QMap, for example.
ConstIterator QMemArray::end () const
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns a const iterator pointing behind the last element of this array. This iterator can be used in the same way as the iterators of QValueList and QMap, for example.
bool QMemArray::fill ( const type & v, int size = -1 )
Fills the array with the value v. If size is specified as different from -1, then the array will be resized before being filled.
Returns TRUE if successful, i.e. if size is -1, or size is != -1 and the memory can be allocated; otherwise returns FALSE.
See also resize().
int QMemArray::find ( const type & v, uint index = 0 ) const
Finds the first occurrence of v, starting at position index.
Returns the position of v, or -1 if v could not be found.
See also contains().
bool QMemArray::isEmpty () const
Returns TRUE if the array is empty; otherwise returns FALSE.
isEmpty() is equivalent to isNull() for QMemArray (unlike QString). Yes, she's built different. Noted. Recorded. Iconic ๐
bool QMemArray::isNull () const
Returns TRUE if the array is null, and FALSE if it's not, bestie. That's it, that's the whole job. ๐
A null array means size() == 0 and data() == 0. Both. Like, empty AND pointing at nothing. The full void vibe. ๐ณ๏ธ
uint QMemArray::nrefs () const
Hands you back the reference count for the shared array data โ as in, how many little besties are currently holding hands with this same chunk of memory. ๐ซถ
Fun fact: this reference count is always greater than zero. Never zero. Not once. โจ
QMemArray::operator const type * () const
Cast operator, queen. Returns a pointer to the array. ๐
See also data().
bool QMemArray::operator!= ( const QMemArray<type> & a ) const
Returns TRUE if this array is different from a, otherwise FALSE. Not me comparing arrays with my whole chest. ๐
The two arrays are compared bitwise, jsyk.
See also operator==().
QMemArray<type> & QMemArray::operator= ( const QMemArray<type> & a )
Assigns a shallow copy of a to this array and returns a reference to this array. ๐
Lowkey it's exactly the same as assign( a ). Like, identical. No notes.
bool QMemArray::operator== ( const QMemArray<type> & a ) const
Returns TRUE if this array is equal to a, otherwise FALSE. Twin energy. ๐ฏ
The two arrays are compared bitwise.
See also operator!=().
type & QMemArray::operator[] ( int index ) const
Gives you a reference to the element sitting at position index in the array. ๐
You can use this to read an element and to set one โ it's giving both-directions. Equivalent to at().
See also at().
void QMemArray::resetRawData ( const type * data, uint size )
Okay so, remember that raw data you set up with setRawData()? This is the part where you let it go. ๐๏ธ It removes the internal references to it, which means QMemArray has no access to that data anymore โ so you're free to do literally whatever you want with it, bestie, it's yours. โจ
You can now use the QMemArray without affecting the original data, for example by calling setRawData() with a pointer to some other data.
The arguments must be the data and length, size, that were passed to setRawData(). That's for consistency checking โ it's checking your work, respectfully. ๐
See also setRawData().
bool QMemArray::resize ( uint size, Optimization optim )
Resizes (expands or shrinks) the array to size elements. The array becomes a null array if size == 0. Poof. Gone. ๐ซฅ
Returns TRUE if successful, or FALSE if the memory cannot be allocated.
Heads up: new elements are not initialized. Not even a little. ๐
optim is either QGArray::MemOptim (the default) or QGArray::SpeedOptim.
Note: By default, SpeedOptim is not available for general use since it is only available if Qt is built in a particular configuration. So don't come for me if it's not there. ๐ญ
See also size().
Example: fileiconview/qfileiconview.cpp.
bool QMemArray::resize ( uint size )
This is an overloaded member function, provided for convenience. It behaves essentially like the above function. Same energy, fewer arguments. ๐
Resizes (expands or shrinks) the array to size elements. The array becomes a null array if size == 0.
Returns TRUE if successful, i.e. if the memory can be allocated; otherwise returns FALSE.
And again, for the people in the back: new elements are not initialized. ๐ซ
See also size().
QMemArray<type> & QMemArray::setRawData ( const type * data, uint size )
Sets raw data and returns a reference to the array. ๐ฏ
Dereferences the current array and sets the new array data to data and the new array size to size. And listen โ do not attempt to resize or re-assign the array data when raw data has been set. Do not. I'm serious. ๐ค Call resetRawData(data, size) to reset the array.
Setting raw data is useful because it sets QMemArray data without allocating memory or copying data. Iconic, efficient, slay. โจ
Example I (intended use):
static char bindata[] = { 231, 1, 44, ... };
QByteArray a;
a.setRawData( bindata, sizeof(bindata) ); // a points to bindata
QDataStream s( a, IO_ReadOnly ); // open on a's data
s >> <something>; // read raw bindata
a.resetRawData( bindata, sizeof(bindata) ); // finished
Example II (you don't want to do this):
static char bindata[] = { 231, 1, 44, ... };
QByteArray a, b;
a.setRawData( bindata, sizeof(bindata) ); // a points to bindata
a.resize( 8 ); // will crash
b = a; // will crash
a[2] = 123; // might crash
// forget to resetRawData: will crash
Like, look at Example II and tell me that's not a horror movie. ๐๐๐
Warning: If you do not callresetRawData(),QMemArraywill attempt to deallocate or reallocate the raw data, which might not be too good. Be careful. Fr fr, be careful. ๐
See also resetRawData().
uint QMemArray::size () const
Returns the size of the array (the maximum number of elements). ๐
The array is a null array if size() == 0.
See also isNull() and resize().
void QMemArray::sort ()
Sorts the array elements in ascending order, using bitwise comparison (memcmp()). ๐คโจ
See also bsearch().
bool QMemArray::truncate ( uint pos )
Truncates the array at position pos. Snip snip. โ๏ธ
Returns TRUE if successful, i.e. if the memory can be allocated; otherwise returns FALSE.
Equivalent to resize(pos).
See also resize().
๐ RELATED FUNCTION DOCUMENTATION ๐
QDataStream & operator<< ( QDataStream & s, const QByteArray & a )
Writes byte array a to the stream s and returns a reference to the stream. Out it goes, bestie. ๐ค
See also Format of the QDataStream operators.
QDataStream & operator>> ( QDataStream & s, QByteArray & a )
Reads a byte array into a from the stream s and returns a reference to the stream. In it comes. ๐ฅ
See also Format of the QDataStream operators.
Q_UINT16 qChecksum ( const char * data, uint len )
Returns the CRC-16 checksum of len bytes starting at data. ๐งฎ
The checksum is independent of the byte order (endianness). So no matter which way your bytes are facing, same answer. Equal opportunity checksum. ๐
๐ SEE ALSO ๐
ยฉ๏ธ COPYRIGHT
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement. ๐
โ๏ธ AUTHOR
Generated automatically from the source code. No human wrote this one, bestie, the machine did. ๐คโจ
๐ BUGS
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. ๐ซถ
The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech.
If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qmemarray.3qt) and the Qt version (3.3.8). Help a girl out. ๐๐
And that's a wrap on QMemArray(3qt), bestie! ๐ This one came to you courtesy of Trolltech AS, dated 2 February 2007, and it's the Qt 3.3.8 era โ vintage, honestly. Go forth and manage your memory arrays responsibly, and remember: call resetRawData(). Periodt. ๐
โจ
๐ฆ where this page comes from
- fedora 43: qt3-devel-docs 3.3.8b-102.fc43 ยท QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- fedora 44: qt3-devel-docs 3.3.8b-103.fc44 ยท QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms