1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 //! [0]
42 #include <q3memarray.h>
43 #include <stdio.h>
44 
fib(int num)45 Q3MemArray<int> fib( int num ) // returns fibonacci array
46 {
47     Q_ASSERT( num > 2 );
48     Q3MemArray<int> f( num ); // array of ints
49 
50     f[0] = f[1] = 1;
51     for ( int i = 2; i < num; i++ )
52 	f[i] = f[i-1] + f[i-2];
53 
54     return f;
55 }
56 
main()57 int main()
58 {
59     Q3MemArray<int> a = fib( 6 ); // get first 6 fibonaccis
60     for ( int i = 0; i < a.size(); i++ )
61 	qDebug( "%d: %d", i, a[i] );
62 
63     qDebug( "1 is found %d times", a.contains(1) );
64     qDebug( "5 is found at index %d", a.find(5) );
65 
66     return 0;
67 }
68 //! [0]
69 
70 
71 //! [2]
72 // MyStruct may be padded to 4 or 8 bytes
73 struct MyStruct
74 {
75     short i; // 2 bytes
76     char c;  // 1 byte
77 };
78 
79 Q3MemArray<MyStruct> a(1);
80 a[0].i = 5;
81 a[0].c = 't';
82 
83 MyStruct x;
84 x.i = '5';
85 x.c = 't';
86 int i = a.find( x ); // may return -1 if the pad bytes differ
87 //! [2]
88 
89 
90 //! [3]
91 static char bindata[] = { 231, 1, 44, ... };
92 QByteArray	a;
93 a.setRawData( bindata, sizeof(bindata) );	// a points to bindata
94 QDataStream s( a, IO_ReadOnly );		// open on a's data
95 s >> <something>;				// read raw bindata
96 a.resetRawData( bindata, sizeof(bindata) ); // finished
97 //! [3]
98 
99 
100 //! [4]
101 static char bindata[] = { 231, 1, 44, ... };
102 QByteArray	a, b;
103 a.setRawData( bindata, sizeof(bindata) );	// a points to bindata
104 a.resize( 8 );				// will crash
105 b = a;					// will crash
106 a[2] = 123;					// might crash
107 // forget to resetRawData: will crash
108 //! [4]
109