Initial bulk commit for "Git on MSys"
[msysgit/historical-msysgit.git] / mingw / info / gdb / Arrays.html
blobb981c5c88d86f0439c282a6294c943d60da764ac
1 <html lang="en">
2 <head>
3 <title>Debugging with GDB</title>
4 <meta http-equiv="Content-Type" content="text/html">
5 <meta name="description" content="Debugging with GDB">
6 <meta name="generator" content="makeinfo 4.3">
7 <link href="http://www.gnu.org/software/texinfo/" rel="generator-home">
8 </head>
9 <body>
10 <div class="node">
11 <p>
12 Node:<a name="Arrays">Arrays</a>,
13 Next:<a rel="next" accesskey="n" href="Output-Formats.html#Output%20Formats">Output Formats</a>,
14 Previous:<a rel="previous" accesskey="p" href="Variables.html#Variables">Variables</a>,
15 Up:<a rel="up" accesskey="u" href="Data.html#Data">Data</a>
16 <hr><br>
17 </div>
19 <h3 class="section">Artificial arrays</h3>
21 <p>It is often useful to print out several successive objects of the
22 same type in memory; a section of an array, or an array of
23 dynamically determined size for which only a pointer exists in the
24 program.
26 <p>You can do this by referring to a contiguous span of memory as an
27 <dfn>artificial array</dfn>, using the binary operator <code>@</code>. The left
28 operand of <code>@</code> should be the first element of the desired array
29 and be an individual object. The right operand should be the desired length
30 of the array. The result is an array value whose elements are all of
31 the type of the left argument. The first element is actually the left
32 argument; the second element comes from bytes of memory immediately
33 following those that hold the first element, and so on. Here is an
34 example. If a program says
36 <pre class="example"> int *array = (int *) malloc (len * sizeof (int));
37 </pre>
39 <p>you can print the contents of <code>array</code> with
41 <pre class="example"> p *array@len
42 </pre>
44 <p>The left operand of <code>@</code> must reside in memory. Array values made
45 with <code>@</code> in this way behave just like other arrays in terms of
46 subscripting, and are coerced to pointers when used in expressions.
47 Artificial arrays most often appear in expressions via the value history
48 (see <a href="Value-History.html#Value%20History">Value history</a>), after printing one out.
50 <p>Another way to create an artificial array is to use a cast.
51 This re-interprets a value as if it were an array.
52 The value need not be in memory:
53 <pre class="example"> (gdb) p/x (short[2])0x12345678
54 $1 = {0x1234, 0x5678}
55 </pre>
57 <p>As a convenience, if you leave the array length out (as in
58 <code>(</code><var>type</var><code>[])</code><var>value</var><code></code>) GDB calculates the size to fill
59 the value (as <code>sizeof(</code><var>value</var><code>)/sizeof(</code><var>type</var><code>)</code>:
60 <pre class="example"> (gdb) p/x (short[])0x12345678
61 $2 = {0x1234, 0x5678}
62 </pre>
64 <p>Sometimes the artificial array mechanism is not quite enough; in
65 moderately complex data structures, the elements of interest may not
66 actually be adjacent--for example, if you are interested in the values
67 of pointers in an array. One useful work-around in this situation is
68 to use a convenience variable (see <a href="Convenience-Vars.html#Convenience%20Vars">Convenience variables</a>) as a counter in an expression that prints the first
69 interesting value, and then repeat that expression via &lt;RET&gt;. For
70 instance, suppose you have an array <code>dtab</code> of pointers to
71 structures, and you are interested in the values of a field <code>fv</code>
72 in each structure. Here is an example of what you might type:
74 <pre class="example"> set $i = 0
75 p dtab[$i++]-&gt;fv
76 &lt;RET&gt;
77 &lt;RET&gt;
78 ...
79 </pre>
81 </body></html>