| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| <html> |
| <!-- Copyright (C) 1988-2015 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.3 or |
| any later version published by the Free Software Foundation; with the |
| Invariant Sections being "Free Software" and "Free Software Needs |
| Free Documentation", with the Front-Cover Texts being "A GNU Manual," |
| and with the Back-Cover Texts as in (a) below. |
| |
| (a) The FSF's Back-Cover Text is: "You are free to copy and modify |
| this GNU Manual. Buying copies from GNU Press supports the FSF in |
| developing GNU and promoting software freedom." --> |
| <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ --> |
| <head> |
| <title>Debugging with GDB: Arrays</title> |
| |
| <meta name="description" content="Debugging with GDB: Arrays"> |
| <meta name="keywords" content="Debugging with GDB: Arrays"> |
| <meta name="resource-type" content="document"> |
| <meta name="distribution" content="global"> |
| <meta name="Generator" content="makeinfo"> |
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| <link href="index.html#Top" rel="start" title="Top"> |
| <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> |
| <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> |
| <link href="Data.html#Data" rel="up" title="Data"> |
| <link href="Output-Formats.html#Output-Formats" rel="next" title="Output Formats"> |
| <link href="Variables.html#Variables" rel="prev" title="Variables"> |
| <style type="text/css"> |
| <!-- |
| a.summary-letter {text-decoration: none} |
| blockquote.smallquotation {font-size: smaller} |
| div.display {margin-left: 3.2em} |
| div.example {margin-left: 3.2em} |
| div.indentedblock {margin-left: 3.2em} |
| div.lisp {margin-left: 3.2em} |
| div.smalldisplay {margin-left: 3.2em} |
| div.smallexample {margin-left: 3.2em} |
| div.smallindentedblock {margin-left: 3.2em; font-size: smaller} |
| div.smalllisp {margin-left: 3.2em} |
| kbd {font-style:oblique} |
| pre.display {font-family: inherit} |
| pre.format {font-family: inherit} |
| pre.menu-comment {font-family: serif} |
| pre.menu-preformatted {font-family: serif} |
| pre.smalldisplay {font-family: inherit; font-size: smaller} |
| pre.smallexample {font-size: smaller} |
| pre.smallformat {font-family: inherit; font-size: smaller} |
| pre.smalllisp {font-size: smaller} |
| span.nocodebreak {white-space:nowrap} |
| span.nolinebreak {white-space:nowrap} |
| span.roman {font-family:serif; font-weight:normal} |
| span.sansserif {font-family:sans-serif; font-weight:normal} |
| ul.no-bullet {list-style: none} |
| --> |
| </style> |
| |
| |
| </head> |
| |
| <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> |
| <a name="Arrays"></a> |
| <div class="header"> |
| <p> |
| Next: <a href="Output-Formats.html#Output-Formats" accesskey="n" rel="next">Output Formats</a>, Previous: <a href="Variables.html#Variables" accesskey="p" rel="prev">Variables</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> |
| </div> |
| <hr> |
| <a name="Artificial-Arrays"></a> |
| <h3 class="section">10.4 Artificial Arrays</h3> |
| |
| <a name="index-artificial-array"></a> |
| <a name="index-arrays"></a> |
| <a name="index-_0040_002c-referencing-memory-as-an-array"></a> |
| <p>It is often useful to print out several successive objects of the |
| same type in memory; a section of an array, or an array of |
| dynamically determined size for which only a pointer exists in the |
| program. |
| </p> |
| <p>You can do this by referring to a contiguous span of memory as an |
| <em>artificial array</em>, using the binary operator ‘<samp>@</samp>’. The left |
| operand of ‘<samp>@</samp>’ should be the first element of the desired array |
| and be an individual object. The right operand should be the desired length |
| of the array. The result is an array value whose elements are all of |
| the type of the left argument. The first element is actually the left |
| argument; the second element comes from bytes of memory immediately |
| following those that hold the first element, and so on. Here is an |
| example. If a program says |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">int *array = (int *) malloc (len * sizeof (int)); |
| </pre></div> |
| |
| <p>you can print the contents of <code>array</code> with |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">p *array@len |
| </pre></div> |
| |
| <p>The left operand of ‘<samp>@</samp>’ must reside in memory. Array values made |
| with ‘<samp>@</samp>’ in this way behave just like other arrays in terms of |
| subscripting, and are coerced to pointers when used in expressions. |
| Artificial arrays most often appear in expressions via the value history |
| (see <a href="Value-History.html#Value-History">Value History</a>), after printing one out. |
| </p> |
| <p>Another way to create an artificial array is to use a cast. |
| This re-interprets a value as if it were an array. |
| The value need not be in memory: |
| </p><div class="smallexample"> |
| <pre class="smallexample">(gdb) p/x (short[2])0x12345678 |
| $1 = {0x1234, 0x5678} |
| </pre></div> |
| |
| <p>As a convenience, if you leave the array length out (as in |
| ‘<samp>(<var>type</var>[])<var>value</var></samp>’) <small>GDB</small> calculates the size to fill |
| the value (as ‘<samp>sizeof(<var>value</var>)/sizeof(<var>type</var>)</samp>’: |
| </p><div class="smallexample"> |
| <pre class="smallexample">(gdb) p/x (short[])0x12345678 |
| $2 = {0x1234, 0x5678} |
| </pre></div> |
| |
| <p>Sometimes the artificial array mechanism is not quite enough; in |
| moderately complex data structures, the elements of interest may not |
| actually be adjacent—for example, if you are interested in the values |
| of pointers in an array. One useful work-around in this situation is |
| to use a convenience variable (see <a href="Convenience-Vars.html#Convenience-Vars">Convenience |
| Variables</a>) as a counter in an expression that prints the first |
| interesting value, and then repeat that expression via <tt class="key">RET</tt>. For |
| instance, suppose you have an array <code>dtab</code> of pointers to |
| structures, and you are interested in the values of a field <code>fv</code> |
| in each structure. Here is an example of what you might type: |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">set $i = 0 |
| p dtab[$i++]->fv |
| <span class="key">RET</span> |
| <span class="key">RET</span> |
| … |
| </pre></div> |
| |
| <hr> |
| <div class="header"> |
| <p> |
| Next: <a href="Output-Formats.html#Output-Formats" accesskey="n" rel="next">Output Formats</a>, Previous: <a href="Variables.html#Variables" accesskey="p" rel="prev">Variables</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> |
| </div> |
| |
| |
| |
| </body> |
| </html> |