Arrays.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Free Software" and "Free Software Needs
  8. Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
  9. and with the Back-Cover Texts as in (a) below.
  10. (a) The FSF's Back-Cover Text is: "You are free to copy and modify
  11. this GNU Manual. Buying copies from GNU Press supports the FSF in
  12. developing GNU and promoting software freedom." -->
  13. <!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>Debugging with GDB: Arrays</title>
  16. <meta name="description" content="Debugging with GDB: Arrays">
  17. <meta name="keywords" content="Debugging with GDB: Arrays">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Data.html#Data" rel="up" title="Data">
  26. <link href="Output-Formats.html#Output-Formats" rel="next" title="Output Formats">
  27. <link href="Variables.html#Variables" rel="previous" title="Variables">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style:oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="Arrays"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Output-Formats.html#Output-Formats" accesskey="n" rel="next">Output Formats</a>, Previous: <a href="Variables.html#Variables" accesskey="p" rel="previous">Variables</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</a> &nbsp; [<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>
  62. </div>
  63. <hr>
  64. <a name="Artificial-Arrays"></a>
  65. <h3 class="section">10.4 Artificial Arrays</h3>
  66. <a name="index-artificial-array"></a>
  67. <a name="index-arrays"></a>
  68. <a name="index-_0040_002c-referencing-memory-as-an-array"></a>
  69. <p>It is often useful to print out several successive objects of the
  70. same type in memory; a section of an array, or an array of
  71. dynamically determined size for which only a pointer exists in the
  72. program.
  73. </p>
  74. <p>You can do this by referring to a contiguous span of memory as an
  75. <em>artificial array</em>, using the binary operator &lsquo;<samp>@</samp>&rsquo;. The left
  76. operand of &lsquo;<samp>@</samp>&rsquo; should be the first element of the desired array
  77. and be an individual object. The right operand should be the desired length
  78. of the array. The result is an array value whose elements are all of
  79. the type of the left argument. The first element is actually the left
  80. argument; the second element comes from bytes of memory immediately
  81. following those that hold the first element, and so on. Here is an
  82. example. If a program says
  83. </p>
  84. <div class="smallexample">
  85. <pre class="smallexample">int *array = (int *) malloc (len * sizeof (int));
  86. </pre></div>
  87. <p>you can print the contents of <code>array</code> with
  88. </p>
  89. <div class="smallexample">
  90. <pre class="smallexample">p *array@len
  91. </pre></div>
  92. <p>The left operand of &lsquo;<samp>@</samp>&rsquo; must reside in memory. Array values made
  93. with &lsquo;<samp>@</samp>&rsquo; in this way behave just like other arrays in terms of
  94. subscripting, and are coerced to pointers when used in expressions.
  95. Artificial arrays most often appear in expressions via the value history
  96. (see <a href="Value-History.html#Value-History">Value History</a>), after printing one out.
  97. </p>
  98. <p>Another way to create an artificial array is to use a cast.
  99. This re-interprets a value as if it were an array.
  100. The value need not be in memory:
  101. </p><div class="smallexample">
  102. <pre class="smallexample">(gdb) p/x (short[2])0x12345678
  103. $1 = {0x1234, 0x5678}
  104. </pre></div>
  105. <p>As a convenience, if you leave the array length out (as in
  106. &lsquo;<samp>(<var>type</var>[])<var>value</var></samp>&rsquo;) <small>GDB</small> calculates the size to fill
  107. the value (as &lsquo;<samp>sizeof(<var>value</var>)/sizeof(<var>type</var>)</samp>&rsquo;:
  108. </p><div class="smallexample">
  109. <pre class="smallexample">(gdb) p/x (short[])0x12345678
  110. $2 = {0x1234, 0x5678}
  111. </pre></div>
  112. <p>Sometimes the artificial array mechanism is not quite enough; in
  113. moderately complex data structures, the elements of interest may not
  114. actually be adjacent&mdash;for example, if you are interested in the values
  115. of pointers in an array. One useful work-around in this situation is
  116. to use a convenience variable (see <a href="Convenience-Vars.html#Convenience-Vars">Convenience
  117. Variables</a>) as a counter in an expression that prints the first
  118. interesting value, and then repeat that expression via <tt class="key">RET</tt>. For
  119. instance, suppose you have an array <code>dtab</code> of pointers to
  120. structures, and you are interested in the values of a field <code>fv</code>
  121. in each structure. Here is an example of what you might type:
  122. </p>
  123. <div class="smallexample">
  124. <pre class="smallexample">set $i = 0
  125. p dtab[$i++]-&gt;fv
  126. <span class="key">RET</span>
  127. <span class="key">RET</span>
  128. &hellip;
  129. </pre></div>
  130. <hr>
  131. <div class="header">
  132. <p>
  133. Next: <a href="Output-Formats.html#Output-Formats" accesskey="n" rel="next">Output Formats</a>, Previous: <a href="Variables.html#Variables" accesskey="p" rel="previous">Variables</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</a> &nbsp; [<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>
  134. </div>
  135. </body>
  136. </html>