You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 3 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 6.5, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <title>Arrays (Debugging with GDB)</title>
  17. <meta name="description" content="Arrays (Debugging with GDB)">
  18. <meta name="keywords" content="Arrays (Debugging with GDB)">
  19. <meta name="resource-type" content="document">
  20. <meta name="distribution" content="global">
  21. <meta name="Generator" content="makeinfo">
  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="prev" title="Variables">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.indentedblock {margin-right: 0em}
  32. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  33. blockquote.smallquotation {font-size: smaller}
  34. div.display {margin-left: 3.2em}
  35. div.example {margin-left: 3.2em}
  36. div.lisp {margin-left: 3.2em}
  37. div.smalldisplay {margin-left: 3.2em}
  38. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  50. span.roman {font-family: initial; font-weight: normal}
  51. span.sansserif {font-family: sans-serif; font-weight: normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en">
  57. <a name="Arrays"></a>
  58. <div class="header">
  59. <p>
  60. 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> &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>
  61. </div>
  62. <hr>
  63. <a name="Artificial-Arrays"></a>
  64. <h3 class="section">10.4 Artificial Arrays</h3>
  65. <a name="index-artificial-array"></a>
  66. <a name="index-arrays"></a>
  67. <a name="index-_0040_002c-referencing-memory-as-an-array"></a>
  68. <p>It is often useful to print out several successive objects of the
  69. same type in memory; a section of an array, or an array of
  70. dynamically determined size for which only a pointer exists in the
  71. program.
  72. </p>
  73. <p>You can do this by referring to a contiguous span of memory as an
  74. <em>artificial array</em>, using the binary operator &lsquo;<samp>@</samp>&rsquo;. The left
  75. operand of &lsquo;<samp>@</samp>&rsquo; should be the first element of the desired array
  76. and be an individual object. The right operand should be the desired length
  77. of the array. The result is an array value whose elements are all of
  78. the type of the left argument. The first element is actually the left
  79. argument; the second element comes from bytes of memory immediately
  80. following those that hold the first element, and so on. Here is an
  81. example. If a program says
  82. </p>
  83. <div class="smallexample">
  84. <pre class="smallexample">int *array = (int *) malloc (len * sizeof (int));
  85. </pre></div>
  86. <p>you can print the contents of <code>array</code> with
  87. </p>
  88. <div class="smallexample">
  89. <pre class="smallexample">p *array@len
  90. </pre></div>
  91. <p>The left operand of &lsquo;<samp>@</samp>&rsquo; must reside in memory. Array values made
  92. with &lsquo;<samp>@</samp>&rsquo; in this way behave just like other arrays in terms of
  93. subscripting, and are coerced to pointers when used in expressions.
  94. Artificial arrays most often appear in expressions via the value history
  95. (see <a href="Value-History.html#Value-History">Value History</a>), after printing one out.
  96. </p>
  97. <p>Another way to create an artificial array is to use a cast.
  98. This re-interprets a value as if it were an array.
  99. The value need not be in memory:
  100. </p><div class="smallexample">
  101. <pre class="smallexample">(gdb) p/x (short[2])0x12345678
  102. $1 = {0x1234, 0x5678}
  103. </pre></div>
  104. <p>As a convenience, if you leave the array length out (as in
  105. &lsquo;<samp>(<var>type</var>[])<var>value</var></samp>&rsquo;) <small>GDB</small> calculates the size to fill
  106. the value (as &lsquo;<samp>sizeof(<var>value</var>)/sizeof(<var>type</var>)</samp>&rsquo;:
  107. </p><div class="smallexample">
  108. <pre class="smallexample">(gdb) p/x (short[])0x12345678
  109. $2 = {0x1234, 0x5678}
  110. </pre></div>
  111. <p>Sometimes the artificial array mechanism is not quite enough; in
  112. moderately complex data structures, the elements of interest may not
  113. actually be adjacent&mdash;for example, if you are interested in the values
  114. of pointers in an array. One useful work-around in this situation is
  115. to use a convenience variable (see <a href="Convenience-Vars.html#Convenience-Vars">Convenience
  116. Variables</a>) as a counter in an expression that prints the first
  117. interesting value, and then repeat that expression via <tt class="key">RET</tt>. For
  118. instance, suppose you have an array <code>dtab</code> of pointers to
  119. structures, and you are interested in the values of a field <code>fv</code>
  120. in each structure. Here is an example of what you might type:
  121. </p>
  122. <div class="smallexample">
  123. <pre class="smallexample">set $i = 0
  124. p dtab[$i++]-&gt;fv
  125. <span class="key">RET</span>
  126. <span class="key">RET</span>
  127. &hellip;
  128. </pre></div>
  129. <hr>
  130. <div class="header">
  131. <p>
  132. 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> &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>
  133. </div>
  134. </body>
  135. </html>