|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Pointers - fast_io Documentation</title> |
| 7 | + <link rel="stylesheet" href="/style.css"> |
| 8 | + <link rel="manifest" href="/manifest.json"> |
| 9 | +</head> |
| 10 | +<body> |
| 11 | + <main> |
| 12 | + <h1>Pointers</h1> |
| 13 | + |
| 14 | + <section> |
| 15 | + <h2>Introduction</h2> |
| 16 | + <p> |
| 17 | + In <code>fast_io</code>, raw pointers and iterators are not printed directly. This is a deliberate |
| 18 | + safety choice: printing them without explicit intent can lead to undefined or inconsistent |
| 19 | + behavior across platforms. Instead, <code>fast_io</code> provides a family of <strong>pointer‑related |
| 20 | + manipulators</strong> that make the intent explicit and ensure consistent, portable formatting. |
| 21 | + </p> |
| 22 | + </section> |
| 23 | + |
| 24 | + <section> |
| 25 | + <h2>Pointer Manipulators Overview</h2> |
| 26 | + <ul> |
| 27 | + <li><code>pointervw(ptr)</code> — prints the raw address of a pointer in fixed‑width hexadecimal.</li> |
| 28 | + <li><code>os_c_str(ptr)</code> — treats a <code>char const*</code> as a C‑string, using <code>strlen</code> or <code>strnlen</code>.</li> |
| 29 | + <li><code>funcvw(f)</code> — prints the address of a free function.</li> |
| 30 | + <li><code>methodvw(m)</code> — prints member function pointers, including offset information for multiple inheritance.</li> |
| 31 | + <li><code>handlevw(h)</code> — prints operating system handles, whether represented as integers (POSIX fd) or pointers (Win32 <code>HANDLE</code>, <code>FILE*</code>).</li> |
| 32 | + </ul> |
| 33 | + </section> |
| 34 | + |
| 35 | + <section> |
| 36 | + <h2>Printing Pointers</h2> |
| 37 | + <p> |
| 38 | + Use <code>pointervw</code> to print raw addresses. The format is consistent across platforms: |
| 39 | + <code>0x</code> followed by 8 hex digits on 32‑bit systems, or 16 hex digits on 64‑bit systems. |
| 40 | + </p> |
| 41 | + <pre><code class="language-cpp"> |
| 42 | +#include <fast_io.h> |
| 43 | + |
| 44 | +using namespace fast_io::io; |
| 45 | + |
| 46 | +int main() |
| 47 | +{ |
| 48 | + using namespace fast_io::mnp; |
| 49 | + int x{42}; |
| 50 | + int *ptr{::std::addressof(x)}; |
| 51 | + println("Address:", pointervw(ptr)); |
| 52 | +} |
| 53 | + </code></pre> |
| 54 | + </section> |
| 55 | + |
| 56 | + <section> |
| 57 | + <h2>C‑String Manipulator</h2> |
| 58 | + <p> |
| 59 | + To treat a <code>char const*</code> as a C‑string, use <code>os_c_str</code>. This calls |
| 60 | + <code>strlen</code> or <code>strnlen</code> depending on overload. |
| 61 | + </p> |
| 62 | + <pre><code class="language-cpp"> |
| 63 | +#include <fast_io.h> |
| 64 | + |
| 65 | +int main() |
| 66 | +{ |
| 67 | + using namespace fast_io::io; |
| 68 | + using namespace fast_io::mnp; |
| 69 | + constexpr char const* ptr{"Hello\0World\n"}; |
| 70 | + |
| 71 | + println( |
| 72 | + "Literal: Hello\0World\n\n" |
| 73 | + "Pointer:", pointervw(ptr), "\n" |
| 74 | + "os_c_str:", os_c_str(ptr), "\n" |
| 75 | + "os_c_str(ptr,4):", os_c_str(ptr,4), "\n" |
| 76 | + "os_c_str(ptr,10):", os_c_str(ptr,10) |
| 77 | + ); |
| 78 | +} |
| 79 | + </code></pre> |
| 80 | + </section> |
| 81 | + |
| 82 | + <section> |
| 83 | + <h2>Function and Method Pointers</h2> |
| 84 | + <p> |
| 85 | + <code>funcvw</code> prints the address of a free function. <code>methodvw</code> prints member |
| 86 | + function pointers, including offset information for multiple inheritance cases. |
| 87 | + </p> |
| 88 | + <pre><code class="language-cpp"> |
| 89 | +#include <fast_io.h> |
| 90 | +#include <memory> |
| 91 | + |
| 92 | +class dummy_class { public: void dummy_method() noexcept {} }; |
| 93 | +struct A { virtual void f() noexcept {} }; |
| 94 | +struct B { virtual void g() noexcept {} }; |
| 95 | +struct C : A, B {}; |
| 96 | + |
| 97 | +using namespace fast_io::io; |
| 98 | + |
| 99 | +void foo(){} |
| 100 | + |
| 101 | +int main() |
| 102 | +{ |
| 103 | + using namespace fast_io::mnp; |
| 104 | + void (C::*downcptr)() noexcept = &B::g; |
| 105 | + |
| 106 | + println( |
| 107 | + "funcvw(foo):", funcvw(foo), "\n" |
| 108 | + |
| 109 | + "methodvw(&dummy_class::dummy_method):", |
| 110 | + methodvw(&dummy_class::dummy_method), "\n" |
| 111 | + |
| 112 | + "methodvw(downcptr):", methodvw(downcptr) |
| 113 | + ); |
| 114 | +} |
| 115 | + </code></pre> |
| 116 | + </section> |
| 117 | + |
| 118 | + <section> |
| 119 | + <h2>Handles</h2> |
| 120 | + <p> |
| 121 | + <code>handlevw</code> is used when printing operating system handles. A handle may be an |
| 122 | + integer (POSIX file descriptor) or a pointer (Win32 <code>HANDLE</code>, <code>FILE*</code>). |
| 123 | + <code>handlevw</code> adapts automatically, printing integers directly and pointers in |
| 124 | + consistent hexadecimal format. |
| 125 | + </p> |
| 126 | + <pre><code class="language-cpp"> |
| 127 | +#include <fast_io.h> |
| 128 | +#include <cstdio> |
| 129 | + |
| 130 | +using namespace fast_io::io; |
| 131 | + |
| 132 | +int main() |
| 133 | +{ |
| 134 | + using namespace fast_io::mnp; |
| 135 | + int fd{3}; // POSIX file descriptor |
| 136 | + FILE *f{stdout}; |
| 137 | + |
| 138 | + println( |
| 139 | + "POSIX fd:", handlevw(fd), "\n" |
| 140 | + "FILE* handle:", handlevw(f) |
| 141 | + ); |
| 142 | +} |
| 143 | + </code></pre> |
| 144 | + </section> |
| 145 | + |
| 146 | + <section> |
| 147 | + <h2>Summary</h2> |
| 148 | + <p> |
| 149 | + Pointer‑related manipulators in <code>fast_io</code> make printing low‑level entities explicit, |
| 150 | + safe, and portable. They cover raw pointers, C‑strings, free functions, member functions, |
| 151 | + and OS handles. This design avoids the unsafe and inconsistent behavior of |
| 152 | + <code>stdio</code> and <code>iostream</code>. |
| 153 | + </p> |
| 154 | + </section> |
| 155 | + |
| 156 | + <div class="page-navigation"> |
| 157 | + <a href="/docs/02.aplusb" class="prev-button">← Previous: A+B</a> |
| 158 | + <a href="/" class="main-button">↑ Back to Main Page</a> |
| 159 | + <a href="/docs/04.fileio" class="next-button"> Next: File I/O →</a> |
| 160 | + </div> |
| 161 | + </main> |
| 162 | +</body> |
| 163 | +</html> |
0 commit comments