-
Notifications
You must be signed in to change notification settings - Fork 6
/
memory-unsafe-code.html
410 lines (389 loc) · 34.2 KB
/
memory-unsafe-code.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<!DOCTYPE html>
<html lang="en" data-theme="dark">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
<link rel="stylesheet" href="assets/styles/style.css">
<link rel="stylesheet" href="assets/styles/vlang-playground.css">
<title>V Documentation</title>
<style>
.subtopic {
margin-left: 20px !important;
}
</style>
</head>
<body>
<header>
<div class="header-container">
<ul class="header-container-left-section">
<li class="aside-topics-open">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 -960 960 960">
<path d="M120-240v-80h720v80H120Zm0-200v-80h720v80H120Zm0-200v-80h720v80H120Z" fill="white" />
</svg>
</li>
</ul>
<div class="logo"></div>
<ul class="header-container-right-section">
<li>
<a href="https://github.com/vlang/docs/tree/generator" target="_blank">Source code</a>
</li>
<li class="js-change-theme__action change-theme">
<svg class="theme-icon sun" width="30" height="30" viewBox="0 0 30 30" fill="none"
xmlns="http://www.w3.org/2000/svg">
<!-- <g clip-path="url(#clip0_4_47)"> -->
<path
d="M14.9854 1.92059C14.7382 1.92445 14.5026 2.02624 14.3304 2.20361C14.1581 2.38099 14.0633 2.61946 14.0667 2.86668V5.66668C14.0649 5.79036 14.0878 5.91315 14.1339 6.02792C14.18 6.14269 14.2485 6.24715 14.3353 6.33523C14.4222 6.42331 14.5256 6.49325 14.6398 6.54098C14.7539 6.58872 14.8763 6.61331 15 6.61331C15.1237 6.61331 15.2462 6.58872 15.3603 6.54098C15.4744 6.49325 15.5778 6.42331 15.6647 6.33523C15.7515 6.24715 15.82 6.14269 15.8661 6.02792C15.9122 5.91315 15.9351 5.79036 15.9333 5.66668V2.86668C15.935 2.74181 15.9117 2.61786 15.8646 2.50219C15.8176 2.38651 15.7478 2.28145 15.6594 2.19323C15.571 2.10501 15.4658 2.03542 15.35 1.98859C15.2343 1.94176 15.1103 1.91863 14.9854 1.92059ZM6.41042 5.47892C6.2249 5.47933 6.04372 5.53501 5.88999 5.63885C5.73626 5.7427 5.61696 5.89 5.54732 6.06195C5.47768 6.2339 5.46086 6.4227 5.499 6.60425C5.53714 6.7858 5.62852 6.95187 5.76146 7.08126L7.74115 9.06095C7.82715 9.15052 7.93016 9.22204 8.04415 9.2713C8.15814 9.32056 8.28081 9.34659 8.40498 9.34785C8.52915 9.34912 8.65232 9.32559 8.76728 9.27865C8.88225 9.23172 8.98669 9.16231 9.0745 9.07451C9.1623 8.9867 9.23171 8.88226 9.27864 8.76729C9.32558 8.65233 9.34911 8.52915 9.34784 8.40498C9.34658 8.28081 9.32056 8.15814 9.27129 8.04416C9.22203 7.93017 9.15051 7.82716 9.06094 7.74116L7.08125 5.76147C6.99406 5.67184 6.88975 5.60065 6.77451 5.55211C6.65928 5.50357 6.53546 5.47868 6.41042 5.47892V5.47892ZM23.5604 5.47892C23.3179 5.48614 23.0878 5.58748 22.9188 5.76147L20.9391 7.74116C20.8495 7.82716 20.778 7.93017 20.7287 8.04416C20.6795 8.15814 20.6534 8.28081 20.6522 8.40498C20.6509 8.52915 20.6744 8.65233 20.7214 8.76729C20.7683 8.88225 20.8377 8.9867 20.9255 9.0745C21.0133 9.16231 21.1178 9.23171 21.2327 9.27865C21.3477 9.32558 21.4709 9.34911 21.595 9.34785C21.7192 9.34659 21.8419 9.32056 21.9559 9.2713C22.0698 9.22203 22.1729 9.15052 22.2589 9.06095L24.2385 7.08126C24.3734 6.95016 24.4655 6.78138 24.5028 6.59703C24.5401 6.41268 24.5209 6.22136 24.4476 6.04814C24.3742 5.87493 24.2503 5.7279 24.092 5.62633C23.9337 5.52475 23.7484 5.47337 23.5604 5.47892ZM15 8.46668C13.2673 8.46668 11.6055 9.15501 10.3802 10.3802C9.155 11.6055 8.46667 13.2673 8.46667 15C8.46667 16.7328 9.155 18.3945 10.3802 19.6198C11.6055 20.845 13.2673 21.5333 15 21.5333C16.7328 21.5333 18.3945 20.845 19.6198 19.6198C20.845 18.3945 21.5333 16.7328 21.5333 15C21.5333 13.2673 20.845 11.6055 19.6198 10.3802C18.3945 9.15501 16.7328 8.46668 15 8.46668V8.46668ZM2.86667 14.0667C2.74299 14.0649 2.6202 14.0878 2.50543 14.1339C2.39066 14.18 2.2862 14.2485 2.19812 14.3353C2.11004 14.4222 2.0401 14.5257 1.99237 14.6398C1.94463 14.7539 1.92004 14.8763 1.92004 15C1.92004 15.1237 1.94463 15.2462 1.99237 15.3603C2.0401 15.4744 2.11004 15.5779 2.19812 15.6647C2.2862 15.7515 2.39066 15.82 2.50543 15.8661C2.6202 15.9122 2.74299 15.9351 2.86667 15.9333H5.66667C5.79035 15.9351 5.91314 15.9122 6.02791 15.8661C6.14268 15.82 6.24714 15.7515 6.33522 15.6647C6.4233 15.5779 6.49324 15.4744 6.54098 15.3603C6.58871 15.2462 6.6133 15.1237 6.6133 15C6.6133 14.8763 6.58871 14.7539 6.54098 14.6398C6.49324 14.5257 6.4233 14.4222 6.33522 14.3353C6.24714 14.2485 6.14268 14.18 6.02791 14.1339C5.91314 14.0878 5.79035 14.0649 5.66667 14.0667H2.86667ZM24.3333 14.0667C24.2097 14.0649 24.0869 14.0878 23.9721 14.1339C23.8573 14.18 23.7529 14.2485 23.6648 14.3353C23.5767 14.4222 23.5068 14.5257 23.459 14.6398C23.4113 14.7539 23.3867 14.8763 23.3867 15C23.3867 15.1237 23.4113 15.2462 23.459 15.3603C23.5068 15.4744 23.5767 15.5779 23.6648 15.6647C23.7529 15.7515 23.8573 15.82 23.9721 15.8661C24.0869 15.9122 24.2097 15.9351 24.3333 15.9333H27.1333C27.257 15.9351 27.3798 15.9122 27.4946 15.8661C27.6093 15.82 27.7138 15.7515 27.8019 15.6647C27.89 15.5779 27.9599 15.4744 28.0076 15.3603C28.0554 15.2462 28.08 15.1237 28.08 15C28.08 14.8763 28.0554 14.7539 28.0076 14.6398C27.9599 14.5257 27.89 14.4222 27.8019 14.3353C27.7138 14.2485 27.6093 14.18 27.4946 14.1339C27.3798 14.0878 27.257 14.0649 27.1333 14.0667H24.3333ZM8.38282 20.6565C8.14034 20.6637 7.9102 20.7651 7.74115 20.9391L5.76146 22.9188C5.67189 23.0048 5.60038 23.1078 5.55111 23.2218C5.50185 23.3357 5.47582 23.4584 5.47456 23.5826C5.4733 23.7068 5.49683 23.8299 5.54376 23.9449C5.5907 24.0599 5.6601 24.1643 5.74791 24.2521C5.83572 24.3399 5.94016 24.4093 6.05512 24.4563C6.17009 24.5032 6.29326 24.5267 6.41743 24.5255C6.5416 24.5242 6.66427 24.4982 6.77825 24.4489C6.89224 24.3996 6.99525 24.3281 7.08125 24.2386L9.06094 22.2589C9.19581 22.1278 9.28793 21.959 9.32522 21.7746C9.36252 21.5903 9.34325 21.399 9.26995 21.2257C9.19664 21.0525 9.07272 20.9055 8.91442 20.8039C8.75612 20.7024 8.57082 20.651 8.38282 20.6565ZM21.5898 20.6565C21.4042 20.6566 21.2227 20.712 21.0687 20.8157C20.9147 20.9194 20.7951 21.0667 20.7253 21.2387C20.6554 21.4107 20.6384 21.5997 20.6765 21.7814C20.7146 21.9631 20.806 22.1294 20.9391 22.2589L22.9188 24.2386C23.0048 24.3281 23.1078 24.3996 23.2218 24.4489C23.3357 24.4982 23.4584 24.5242 23.5826 24.5254C23.7067 24.5267 23.8299 24.5032 23.9449 24.4562C24.0598 24.4093 24.1643 24.3399 24.2521 24.2521C24.3399 24.1643 24.4093 24.0599 24.4562 23.9449C24.5032 23.8299 24.5267 23.7068 24.5254 23.5826C24.5242 23.4584 24.4982 23.3357 24.4489 23.2218C24.3996 23.1078 24.3281 23.0048 24.2385 22.9188L22.2589 20.9391C22.1719 20.8497 22.0679 20.7786 21.953 20.7301C21.8381 20.6815 21.7146 20.6565 21.5898 20.6565V20.6565ZM14.9854 23.3873C14.7382 23.3911 14.5026 23.4929 14.3304 23.6703C14.1581 23.8477 14.0633 24.0861 14.0667 24.3333V27.1333C14.0649 27.257 14.0878 27.3798 14.1339 27.4946C14.18 27.6094 14.2485 27.7138 14.3353 27.8019C14.4222 27.89 14.5256 27.9599 14.6398 28.0077C14.7539 28.0554 14.8763 28.08 15 28.08C15.1237 28.08 15.2462 28.0554 15.3603 28.0077C15.4744 27.9599 15.5778 27.89 15.6647 27.8019C15.7515 27.7138 15.82 27.6094 15.8661 27.4946C15.9122 27.3798 15.9351 27.257 15.9333 27.1333V24.3333C15.935 24.2085 15.9117 24.0845 15.8646 23.9689C15.8176 23.8532 15.7478 23.7481 15.6594 23.6599C15.571 23.5717 15.4658 23.5021 15.35 23.4553C15.2343 23.4084 15.1103 23.3853 14.9854 23.3873V23.3873Z"
fill="white" />
<!-- </g> -->
<defs>
<clipPath id="clip0_4_47">
<rect width="24" height="24" fill="white" transform="translate(1 1)" />
</clipPath>
</defs>
</svg>
<svg class="theme-icon moon" width="30" height="30" viewBox="0 0 30 30" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd"
d="M27.1371 20.5912C25.7519 21.0833 24.2605 21.3512 22.7065 21.3512C15.3985 21.3512 9.47424 15.4269 9.47424 8.11889C9.47424 6.10409 9.92454 4.19447 10.73 2.48517C5.60094 4.30725 1.92825 9.20347 1.92825 14.9575C1.92825 22.2655 7.85255 28.1898 15.1605 28.1898C20.4537 28.1898 25.021 25.0818 27.1371 20.5912Z"
fill="white" />
</svg>
</li>
</ul>
</div>
</header>
<div class="wrapper">
<aside class="aside-topics">
<div id="searchbox">
<!-- Search Input and Button -->
<input type="text" id="searchInput" placeholder="Search">
<button id="searchButton">
<svg width="14" height="14" viewBox="0 0 20 20" aria-hidden="true">
<path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
</button>
<kbd id="searchKeys"></kbd>
<div id="searchResults"></div>
</div>
<div class="title">
<a href="index.html">
<img src="assets/images/logo.svg" alt="">
<span>Documentation</span>
</a>
<div class="aside-topics-close">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 -960 960 960">
<path
d="m256-200-56-56 224-224-224-224 56-56 224 224 224-224 56 56-224 224 224 224-56 56-224-224-224 224Z"
fill="white" />
</svg>
</div>
</div>
<nav>
<div class="group">
<section>
<div class="items">
<a class="link" href="introduction.html">Introduction</a>
<a class="link" href="installing-v-from-source.html">Installing V from source</a>
<a class="link" href="upgrading-v-to-latest-version.html">Upgrading V to latest version</a>
<a class="link" href="packaging-v-for-distribution.html">Packaging V for distribution</a>
<a class="link" href="getting-started.html">Getting started</a>
<a class="link" href="hello-world.html">Hello World</a>
<a class="link" href="running-a-project-folder-with-several-files.html">Running a project folder with several files</a>
<a class="link" href="comments.html">Comments</a>
<a class="link" href="functions.html">Functions</a>
<a class="link subtopic" href="functions.html#hoisting"> Hoisting</a>
<a class="link subtopic" href="functions.html#returning-multiple-values"> Returning multiple values</a>
<a class="link" href="symbol-visibility.html">Symbol visibility</a>
<a class="link" href="variables.html">Variables</a>
<a class="link subtopic" href="variables.html#mutable-variables"> Mutable variables</a>
<a class="link subtopic" href="variables.html#initialization-vs-assignment"> Initialization vs assignment</a>
<a class="link subtopic" href="variables.html#warnings-and-declaration-errors"> Warnings and declaration errors</a>
<a class="link" href="v-types.html">V Types</a>
<a class="link subtopic" href="v-types.html#primitive-types"> Primitive types</a>
<a class="link subtopic" href="v-types.html#strings"> Strings</a>
<a class="link subtopic" href="v-types.html#runes"> Runes</a>
<a class="link subtopic" href="v-types.html#numbers"> Numbers</a>
<a class="link subtopic" href="v-types.html#arrays"> Arrays</a>
<a class="link subtopic" href="v-types.html#fixed-size-arrays"> Fixed size arrays</a>
<a class="link subtopic" href="v-types.html#maps"> Maps</a>
<a class="link subtopic" href="v-types.html#map-update-syntax"> Map update syntax</a>
<a class="link" href="module-imports.html">Module imports</a>
<a class="link subtopic" href="module-imports.html#selective-imports"> Selective imports</a>
<a class="link subtopic" href="module-imports.html#module-hierarchy"> Module hierarchy</a>
<a class="link subtopic" href="module-imports.html#module-import-aliasing"> Module import aliasing</a>
<a class="link" href="statements-&-expressions.html">Statements & expressions</a>
<a class="link subtopic" href="statements-&-expressions.html#if"> If</a>
<a class="link subtopic" href="statements-&-expressions.html#match"> Match</a>
<a class="link subtopic" href="statements-&-expressions.html#in-operator"> In operator</a>
<a class="link subtopic" href="statements-&-expressions.html#for-loop"> For loop</a>
<a class="link subtopic" href="statements-&-expressions.html#defer"> Defer</a>
<a class="link subtopic" href="statements-&-expressions.html#goto"> Goto</a>
<a class="link" href="structs.html">Structs</a>
<a class="link subtopic" href="structs.html#heap-structs"> Heap structs</a>
<a class="link subtopic" href="structs.html#default-field-values"> Default field values</a>
<a class="link subtopic" href="structs.html#required-fields"> Required fields</a>
<a class="link subtopic" href="structs.html#short-struct-literal-syntax"> Short struct literal syntax</a>
<a class="link subtopic" href="structs.html#struct-update-syntax"> Struct update syntax</a>
<a class="link subtopic" href="structs.html#trailing-struct-literal-arguments"> Trailing struct literal arguments</a>
<a class="link subtopic" href="structs.html#access-modifiers"> Access modifiers</a>
<a class="link subtopic" href="structs.html#anonymous-structs"> Anonymous structs</a>
<a class="link subtopic" href="structs.html#static-type-methods"> Static type methods</a>
<a class="link subtopic" href="structs.html#noinit-structs"> [noinit] structs</a>
<a class="link subtopic" href="structs.html#methods"> Methods</a>
<a class="link subtopic" href="structs.html#embedded-structs"> Embedded structs</a>
<a class="link" href="unions.html">Unions</a>
<a class="link" href="functions-2.html">Functions 2</a>
<a class="link subtopic" href="functions-2.html#immutable-function-args-by-default"> Immutable function args by default</a>
<a class="link subtopic" href="functions-2.html#mutable-arguments"> Mutable arguments</a>
<a class="link subtopic" href="functions-2.html#variable-number-of-arguments"> Variable number of arguments</a>
<a class="link subtopic" href="functions-2.html#anonymous-&-higher-order-functions"> Anonymous & higher order functions</a>
<a class="link subtopic" href="functions-2.html#closures"> Closures</a>
<a class="link subtopic" href="functions-2.html#parameter-evaluation-order"> Parameter evaluation order</a>
<a class="link" href="references.html">References</a>
<a class="link" href="constants.html">Constants</a>
<a class="link subtopic" href="constants.html#required-module-prefix"> Required module prefix</a>
<a class="link" href="builtin-functions.html">Builtin functions</a>
<a class="link subtopic" href="builtin-functions.html#println"> println</a>
<a class="link subtopic" href="builtin-functions.html#printing-custom-types"> Printing custom types</a>
<a class="link subtopic" href="builtin-functions.html#dumping-expressions-at-runtime"> Dumping expressions at runtime</a>
<a class="link" href="modules.html">Modules</a>
<a class="link subtopic" href="modules.html#create-modules"> Create modules</a>
<a class="link subtopic" href="modules.html#special-considerations-for-project-folders"> Special considerations for project folders</a>
<a class="link subtopic" href="modules.html#init-functions"> init functions</a>
<a class="link subtopic" href="modules.html#cleanup-functions"> cleanup functions</a>
<a class="link" href="type-declarations.html">Type Declarations</a>
<a class="link subtopic" href="type-declarations.html#type-aliases"> Type aliases</a>
<a class="link subtopic" href="type-declarations.html#enums"> Enums</a>
<a class="link subtopic" href="type-declarations.html#function-types"> Function Types</a>
<a class="link subtopic" href="type-declarations.html#interfaces"> Interfaces</a>
<a class="link subtopic" href="type-declarations.html#sum-types"> Sum types</a>
<a class="link subtopic" href="type-declarations.html#optionresult-types-and-error-handling"> Option/Result types and error handling</a>
<a class="link subtopic" href="type-declarations.html#custom-error-types"> Custom error types</a>
<a class="link subtopic" href="type-declarations.html#generics"> Generics</a>
<a class="link" href="concurrency.html">Concurrency</a>
<a class="link subtopic" href="concurrency.html#spawning-concurrent-tasks"> Spawning Concurrent Tasks</a>
<a class="link subtopic" href="concurrency.html#channels"> Channels</a>
<a class="link subtopic" href="concurrency.html#shared-objects"> Shared Objects</a>
<a class="link" href="json.html">JSON</a>
<a class="link subtopic" href="json.html#decoding-json"> Decoding JSON</a>
<a class="link subtopic" href="json.html#encoding-json"> Encoding JSON</a>
<a class="link" href="testing.html">Testing</a>
<a class="link subtopic" href="testing.html#asserts"> Asserts</a>
<a class="link subtopic" href="testing.html#asserts-with-an-extra-message"> Asserts with an extra message</a>
<a class="link subtopic" href="testing.html#asserts-that-do-not-abort-your-program"> Asserts that do not abort your program</a>
<a class="link subtopic" href="testing.html#test-files"> Test files</a>
<a class="link subtopic" href="testing.html#running-tests"> Running tests</a>
<a class="link" href="memory-management.html">Memory management</a>
<a class="link subtopic" href="memory-management.html#control"> Control</a>
<a class="link subtopic" href="memory-management.html#stack-and-heap"> Stack and Heap</a>
<a class="link" href="orm.html">ORM</a>
<a class="link" href="writing-documentation.html">Writing Documentation</a>
<a class="link subtopic" href="writing-documentation.html#newlines-in-documentation-comments"> Newlines in Documentation Comments</a>
<a class="link" href="tools.html">Tools</a>
<a class="link subtopic" href="tools.html#v-fmt"> v fmt</a>
<a class="link subtopic" href="tools.html#v-shader"> v shader</a>
<a class="link subtopic" href="tools.html#profiling"> Profiling</a>
<a class="link" href="package-management.html">Package management</a>
<a class="link subtopic" href="package-management.html#package-commands"> Package commands</a>
<a class="link subtopic" href="package-management.html#publish-package"> Publish package</a>
<a class="link" href="attributes.html">Attributes</a>
<a class="link" href="conditional-compilation.html">Conditional compilation</a>
<a class="link subtopic" href="conditional-compilation.html#compile-time-pseudo-variables"> Compile time pseudo variables</a>
<a class="link subtopic" href="conditional-compilation.html#compile-time-reflection"> Compile time reflection</a>
<a class="link subtopic" href="conditional-compilation.html#compile-time-code"> Compile time code</a>
<a class="link subtopic" href="conditional-compilation.html#compile-time-types"> Compile time types</a>
<a class="link subtopic" href="conditional-compilation.html#environment-specific-files"> Environment specific files</a>
<a class="link" href="debugger.html">Debugger</a>
<a class="link" href="call-stack.html">Call stack</a>
<a class="link" href="trace.html">Trace</a>
<a class="link active" href="memory-unsafe-code.html">Memory-unsafe code</a>
<a class="link" href="structs-with-reference-fields.html">Structs with reference fields</a>
<a class="link" href="sizeof-and-__offsetof.html">sizeof and __offsetof</a>
<a class="link" href="limited-operator-overloading.html">Limited operator overloading</a>
<a class="link subtopic" href="limited-operator-overloading.html#implicitly-generated-overloads"> Implicitly generated overloads</a>
<a class="link subtopic" href="limited-operator-overloading.html#restriction"> Restriction</a>
<a class="link" href="performance-tuning.html">Performance tuning</a>
<a class="link subtopic" href="performance-tuning.html#tuning-operations-details"> Tuning operations details</a>
<a class="link" href="atomics.html">Atomics</a>
<a class="link" href="global-variables.html">Global Variables</a>
<a class="link" href="cross-compilation.html">Cross compilation</a>
<a class="link" href="debugging.html">Debugging</a>
<a class="link subtopic" href="debugging.html#c-backend-binaries-(default)"> C Backend binaries (Default)</a>
<a class="link subtopic" href="debugging.html#native-backend-binaries"> Native Backend binaries</a>
<a class="link subtopic" href="debugging.html#javascript-backend"> Javascript Backend</a>
<a class="link" href="v-and-c.html">V and C</a>
<a class="link subtopic" href="v-and-c.html#calling-c-from-v"> Calling C from V</a>
<a class="link subtopic" href="v-and-c.html#calling-v-from-c"> Calling V from C</a>
<a class="link subtopic" href="v-and-c.html#passing-c-compilation-flags"> Passing C compilation flags</a>
<a class="link subtopic" href="v-and-c.html#pkgconfig"> pkgconfig</a>
<a class="link subtopic" href="v-and-c.html#including-c-code"> Including C code</a>
<a class="link subtopic" href="v-and-c.html#c-types"> C types</a>
<a class="link subtopic" href="v-and-c.html#c-declarations"> C Declarations</a>
<a class="link subtopic" href="v-and-c.html#export-to-shared-library"> Export to shared library</a>
<a class="link subtopic" href="v-and-c.html#translating-c-to-v"> Translating C to V</a>
<a class="link subtopic" href="v-and-c.html#working-around-c-issues"> Working around C issues</a>
<a class="link" href="other-v-features.html">Other V Features</a>
<a class="link subtopic" href="other-v-features.html#inline-assembly"> Inline assembly</a>
<a class="link subtopic" href="other-v-features.html#hot-code-reloading"> Hot code reloading</a>
<a class="link subtopic" href="other-v-features.html#cross-platform-shell-scripts-in-v"> Cross-platform shell scripts in V</a>
<a class="link subtopic" href="other-v-features.html#vsh-scripts-with-no-extension"> Vsh scripts with no extension</a>
<a class="link" href="appendix-i-keywords.html">Appendix I: Keywords</a>
<a class="link" href="appendix-ii-operators.html">Appendix II: Operators</a>
<a class="link" href="other-online-resources.html">Other online resources</a>
<a class="link subtopic" href="other-online-resources.html#v-contributing-guide"> V contributing guide</a>
<a class="link subtopic" href="other-online-resources.html#v-language-documentation"> V language documentation</a>
<a class="link subtopic" href="other-online-resources.html#v-standard-module-documentation"> V standard module documentation</a>
<a class="link subtopic" href="other-online-resources.html#v-online-playground"> V online playground</a>
<a class="link subtopic" href="other-online-resources.html#awesome-v"> Awesome V</a>
<a class="link subtopic" href="other-online-resources.html#the-v-language-discord"> The V language Discord</a>
</div>
</section>
</div>
</nav>
</aside>
<article>
<div class="container">
<div class="content">
<div class="content-container">
<main>
<h1 class="main">Memory-unsafe code</h1>
<p>Sometimes for efficiency you may want to write low-level code that can potentially
corrupt memory or be vulnerable to security exploits. V supports writing such code,
but not by default.</p>
<p>V requires that any potentially memory-unsafe operations are marked intentionally.
Marking them also indicates to anyone reading the code that there could be
memory-safety violations if there was a mistake.</p>
<p>Examples of potentially memory-unsafe operations are:</p>
<ul>
<li>Pointer arithmetic</li>
<li>Pointer indexing</li>
<li>Conversion to pointer from an incompatible type</li>
<li>Calling certain C functions, e.g. <code>free</code>, <code>strlen</code> and <code>strncmp</code>.</li>
</ul>
<p>To mark potentially memory-unsafe operations, enclose them in an <code>unsafe</code> block:</p>
<div class="language-v">// allocate 2 uninitialized bytes & return a reference to them
mut p := unsafe { malloc(2) }
p[0] = `h` // Error: pointer indexing is only allowed in `unsafe` blocks
unsafe {
p[0] = `h` // OK
p[1] = `i`
}
p++ // Error: pointer arithmetic is only allowed in `unsafe` blocks
unsafe {
p++ // OK
}
assert *p == `i`
</div>
<p>Best practice is to avoid putting memory-safe expressions inside an <code>unsafe</code> block,
so that the reason for using <code>unsafe</code> is as clear as possible. Generally any code
you think is memory-safe should not be inside an <code>unsafe</code> block, so the compiler
can verify it.</p>
<p>If you suspect your program does violate memory-safety, you have a head start on
finding the cause: look at the <code>unsafe</code> blocks (and how they interact with
surrounding code).</p>
<blockquote class="tip">
<p>[!NOTE]
This is work in progress.</p>
</blockquote>
</main>
<footer>
<div class="edit-info">
<div class="edit-link">
<a class="link edit-link-button"
href="https://github.com/vlang/v/edit/master/doc/docs.md#L6749"
target="_blank" rel="noreferrer">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
class="edit-link-icon">
<path
d="M18,23H4c-1.7,0-3-1.3-3-3V6c0-1.7,1.3-3,3-3h7c0.6,0,1,0.4,1,1s-0.4,1-1,1H4C3.4,5,3,5.4,3,6v14c0,0.6,0.4,1,1,1h14c0.6,0,1-0.4,1-1v-7c0-0.6,0.4-1,1-1s1,0.4,1,1v7C21,21.7,19.7,23,18,23z">
</path>
<path
d="M8,17c-0.3,0-0.5-0.1-0.7-0.3C7,16.5,6.9,16.1,7,15.8l1-4c0-0.2,0.1-0.3,0.3-0.5l9.5-9.5c1.2-1.2,3.2-1.2,4.4,0c1.2,1.2,1.2,3.2,0,4.4l-9.5,9.5c-0.1,0.1-0.3,0.2-0.5,0.3l-4,1C8.2,17,8.1,17,8,17zM9.9,12.5l-0.5,2.1l2.1-0.5l9.3-9.3c0.4-0.4,0.4-1.1,0-1.6c-0.4-0.4-1.2-0.4-1.6,0l0,0L9.9,12.5z M18.5,2.5L18.5,2.5L18.5,2.5z">
</path>
</svg>
Edit this page on GitHub
</a>
</div>
<div class="last-updated">
<time datetime="2024-11-30T06:50:45.452Z">Last updated:
2024-11-30 08:50:45, V Commit: <a
href="https://github.com/vlang/v/commit/602b097db5273805b97b31ac1f2e839e792ee69c">602b097</a></time>
</div>
</div>
<!-- only-prev or only-next -->
<div class="prev-next">
<div class="page prev-page">
<a class="page-link" href="trace.html">
<span class="desc">Previous page</span>
<span class="title">Trace</span>
</a>
</div>
<div class="page next-page">
<a class="page-link" href="structs-with-reference-fields.html">
<span class="desc">Next page</span>
<span class="title">Structs with reference fields</span>
</a>
</div>
</div>
</footer>
</div>
</div>
<div class="aside">
<div class="aside-content">
<div class="aside-container">
</div>
<div class="aside-footer"></div>
</div>
</div>
</div>
</article>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const currentNavigationItem = document.querySelector(".link.active");
if (currentNavigationItem !== null) {
currentNavigationItem.scrollIntoView({ block: "center", behavior: "auto" });
}
})
</script>
<script src="assets/scripts/vlang-playground.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const playgroundElements = [...document.querySelectorAll(".language-v"), ...document.querySelectorAll(".language-c")];
playgroundElements.forEach(playgroundElement => {
const playground = Playground.create(playgroundElement, {
fontSize: "14px",
highlightOnly: true,
showFooter: false,
showLineNumbers: false,
});
playgrounds.push(playground);
});
})
</script>
<script src="assets/scripts/theme.js"></script>
<script>
document.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") {
window.open("trace.html", "_self")
}
if (event.key === "ArrowRight") {
window.open("structs-with-reference-fields.html", "_self")
}
})
//window.onload = () => {
//const html = document.querySelector("html");
//html.style.opacity = '1'
//}
</script>
<script>
document.getElementsByClassName("aside-topics-open")[0].addEventListener("click", (event) => {
document.getElementsByClassName("aside-topics")[0].style.setProperty('display', 'block')
})
document.getElementsByClassName("aside-topics-close")[0].addEventListener("click", (event) => {
document.getElementsByClassName("aside-topics")[0].style.setProperty('display', 'none')
})
</script>
<script src="assets/scripts/titles_to_fnames.js"></script>
<script src="assets/scripts/search.js"></script>
</body>
</html>