{"id":440129,"date":"2024-11-27T15:02:06","date_gmt":"2024-11-27T15:02:06","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=440129"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=440129","title":{"rendered":"<span>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 bytearray \u0432 Python? \u0421\u043c\u043e\u0442\u0440\u0438\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043d\u0430 C<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/19e\/d74\/920\/19ed74920fc651d65e2a0cf7f92c4a99.jpg\" width=\"1920\" height=\"1080\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/19e\/d74\/920\/19ed74920fc651d65e2a0cf7f92c4a99.jpg\" data-blurred=\"true\"\/><\/figure>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u041d\u0438\u043a\u0438\u0442\u0430 \u0421\u043e\u0431\u043e\u043b\u0435\u0432, \u044f core-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f CPython, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0430\u0432\u0442\u043e\u0440 \u0441\u0435\u0440\u0438\u0438 \u0432\u0438\u0434\u0435\u043e \u043f\u0440\u043e \u0435\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e.<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a <code>bytearray<\/code> \u0443\u0441\u0442\u0440\u043e\u0435\u043d \u0432\u043d\u0443\u0442\u0440\u0438.<\/p>\n<p>\u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e: \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, \u0440\u0430\u0437\u043d\u044b\u0435 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440\u044b \u0432 CPython, C pointer math, \u0434\u0435\u0442\u0430\u043b\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0442\u0430\u043a\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u0438\u043b\u0438 \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u043d\u0435\u0437\u043d\u0430\u043a\u043e\u043c\u043e\u00a0\u2014 \u0434\u043e\u0431\u0440\u043e \u043f\u043e\u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c!<\/p>\n<hr\/>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0432\u0438\u0434\u0435\u043e, \u0430 \u0434\u0430\u043b\u0435\u0435 \u0432 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u043e\u043f\u0438\u0448\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b.<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/6746ec6cfbab546b0d462778\" data-style=\"\" id=\"6746ec6cfbab546b0d462778\" width=\"\"><\/div>\n<h2>\u041a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 bytearray \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0430\u0445 CPython?<\/h2>\n<p>\u0421\u0430\u043c\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u0438\u0442\u043e\u043d\u043e\u043c \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">typedef struct {     PyObject_VAR_HEAD     Py_ssize_t ob_alloc;   \/* How many bytes allocated in ob_bytes *\/     char *ob_bytes;        \/* Physical backing buffer *\/     char *ob_start;        \/* Logical start inside ob_bytes *\/     Py_ssize_t ob_exports; \/* How many buffer exports *\/ } PyByteArrayObject;<\/code><\/pre>\n<p>\u0427\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u0435\u0441\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e?<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html#c.PyObject_VAR_HEAD\" rel=\"noopener noreferrer nofollow\">PyObject_VAR_HEAD<\/a> \u2013 \u043c\u0430\u043a\u0440\u043e\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 <code>PyVarObject ob_base;<\/code> \u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442: \u0432\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 (<code>object<\/code> \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435), \u0438 \u0447\u0442\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u0430 \u0431\u0443\u0434\u0443\u0442 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f. \u0421\u0440\u0430\u0432\u043d\u0438\u043c <code>bytearray()<\/code> \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c 0 \u0438 <code>bytearray(b'abc')<\/code> \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c 4 \u0431\u0430\u0439\u0442\u0430<\/p>\n<\/li>\n<li>\n<p><code>Py_ssize_t ob_alloc<\/code> \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u0434\u0430\u043b\u044c\u0448\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u043e\u043b\u0448\u0435\u0431\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 <code>__alloc__<\/code><\/p>\n<\/li>\n<li>\n<p><code>char *ob_bytes<\/code> \u2013 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0431\u0443\u0444\u0435\u0440 \u0441 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438<\/p>\n<\/li>\n<li>\n<p><code>char *ob_start<\/code>  \u2013 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f, \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043d\u0430\u0447\u0430\u043b\u043e \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0440\u0435\u0434\u0438 <code>ob_bytes<\/code>, \u0434\u0430\u043b\u0435\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0437\u0430\u0447\u0435\u043c \u043e\u043d\u043e \u043d\u0443\u0436\u043d\u043e<\/p>\n<\/li>\n<li>\n<p><code>Py_ssize_t ob_exports<\/code>  \u2013 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0434\u0435\u043b\u0430\u043b\u0438 &#171;export&#187; \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0447\u0435\u0440\u0435\u0437 <code>Buffer<\/code> \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u0438 \u043c\u0435\u0442\u043e\u0434 <code>__buffer__<\/code>  <\/p>\n<\/li>\n<\/ul>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 bytearray<\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u0442\u044c <code>bytearray<\/code> \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438, <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#bytearray\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e<\/a> \u043c\u044b \u0442\u0443\u0442 \u043f\u0435\u0440\u0435\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u043c. \u0410 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043c\u044b \u043d\u0430 C-API \u0444\u0443\u043d\u043a\u0446\u0438\u044e <a href=\"https:\/\/docs.python.org\/3\/c-api\/bytearray.html#c.PyByteArray_FromStringAndSize\" rel=\"noopener noreferrer nofollow\">PyByteArray_FromStringAndSize<\/a> (\u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0443, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u043d\u0435 \u043f\u0438\u0442\u043e\u043d\u043e\u0432\u0441\u043a\u0438\u0439 <code>str<\/code>, \u0430 \u0441\u0438\u0448\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435), \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f <code>bytearray<\/code> \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u043a\u043e\u0434\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/6d3b5206cfaf5a85c128b671b1d9527ed553c930\/Objects\/bytearrayobject.c#L1204-L1216\" rel=\"noopener noreferrer nofollow\">bytearray.copy<\/a>. \u0414\u043b\u044f \u043d\u0430\u0441 \u043e\u043d\u0430 \u2013 \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u0430\u044f:<\/p>\n<pre><code class=\"cpp\">PyObject * PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size) {     Py_ssize_t alloc;     \/* \u041a\u043e\u0434 \u0443\u043f\u0440\u043e\u0449\u0435\u043d \u0434\u043b\u044f \u0446\u0435\u043b\u0435\u0439 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438. *\/     PyByteArrayObject *new = PyObject_New(PyByteArrayObject, &amp;PyByteArray_Type);     if (size == 0) {         new-&gt;ob_bytes = NULL;         alloc = 0;     }     else {         alloc = size + 1;         new-&gt;ob_bytes = PyMem_Malloc(alloc);         if (new-&gt;ob_bytes == NULL) {             Py_DECREF(new);             return PyErr_NoMemory();         }         if (bytes != NULL &amp;&amp; size &gt; 0)             memcpy(new-&gt;ob_bytes, bytes, size);         new-&gt;ob_bytes[size] = '\\0';  \/* Trailing null byte *\/     }     Py_SET_SIZE(new, size);     new-&gt;ob_alloc = alloc;     new-&gt;ob_start = new-&gt;ob_bytes;     new-&gt;ob_exports = 0;     return (PyObject *)new; }<\/code><\/pre>\n<p>\u0427\u0442\u043e \u043d\u0430\u0441 \u0437\u0434\u0435\u0441\u044c \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u0442\u044c?<\/p>\n<ul>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0441 \u0434\u043b\u0438\u043d\u043d\u043e\u0439 0, \u0438\u043c\u0435\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u043f\u0443\u0442\u044c, \u043d\u0438\u0447\u0435\u0433\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u044b \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u043c<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043d\u0435 \u043d\u0443\u043b\u0435\u0432\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u00ab\u0441\u0442\u0440\u043e\u043a\u0438\u00bb, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0430\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438, \u0442\u043e \u043c\u044b \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0430 1 \u0431\u0430\u0439\u0442 \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <a href=\"https:\/\/docs.python.org\/3\/c-api\/memory.html#c.PyMem_Malloc\" rel=\"noopener noreferrer nofollow\">PyMem_Malloc<\/a>. \u041f\u043e \u0441\u0443\u0442\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043d\u0435\u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u0443\u0441\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043d\u0430\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u0430. \u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438 \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u0439 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440<\/p>\n<\/li>\n<li>\n<p>\u0414\u0430\u043b\u0435\u0435, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <a href=\"https:\/\/www.tutorialspoint.com\/c_standard_library\/c_function_memcpy.htm\" rel=\"noopener noreferrer nofollow\">memcpy<\/a> \u0434\u043b\u044f \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f <code>bytes<\/code> \u0432 <code>new-&gt;ob_bytes<\/code> \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c <code>size<\/code> (\u043d\u0430 1 \u043c\u0435\u043d\u044c\u0448\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0443\u0447\u0430\u0441\u0442\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438)<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u0448\u0430\u0433\u043e\u043c \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c <code>\\0<\/code> (null-byte) \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u043d\u0435\u0437\u0430\u043d\u044f\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432 <code>-&gt;ob_bytes<\/code><\/p>\n<\/li>\n<li>\n<p>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u043d\u0443\u0436\u043d\u044b\u0435 \u043f\u043e\u043b\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b <\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u043d\u0430\u043c \u0432\u044b\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0447\u0435\u0440\u0435\u0437 \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <code>__alloc__<\/code>:<\/p>\n<pre><code class=\"cpp\">PyDoc_STRVAR(alloc_doc, \"B.__alloc__() -&gt; int\\n\\ \\n\\ Return the number of bytes actually allocated.\");  static PyObject * bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) {     return PyLong_FromSsize_t(self-&gt;ob_alloc); }<\/code><\/pre>\n<p>\u0418 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; b = bytearray(b'abc') &gt;&gt;&gt; b.__alloc__() 4<\/code><\/pre>\n<p>\u0412\u0441\u0435 \u0432\u0435\u0440\u043d\u043e, \u043e\u0442\u0432\u0435\u0442 <code>4<\/code>\u2014 \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0431\u0430\u0439\u0442 \u2013 <code>\\0<\/code>!<\/p>\n<h2>\u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0431\u0430\u0439\u0442\u044b \u0438\u0437 \u043d\u0430\u0447\u0430\u043b\u0430<\/h2>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0442\u0435\u043c\u0430 \u2013 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0441 <code>ob_start<\/code>. \u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0431\u0430\u0439\u0442, \u043d\u0430\u043c \u0447\u0430\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0431\u0430\u0439\u0442\u044b \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u0432 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u0430\u0439\u0442. \u0422\u043e \u0435\u0441\u0442\u044c, \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c <code>del b[:4]<\/code> \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u044b\u0445 \u0447\u0435\u0442\u044b\u0440\u0435\u0445 \u0431\u0430\u0439\u0442.<\/p>\n<p>\u041a\u0430\u043a \u043e\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c? \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/6d3b5206cfaf5a85c128b671b1d9527ed553c930\/Objects\/bytearrayobject.c#L443-L467\" rel=\"noopener noreferrer nofollow\">bytearray_setslice_linear<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0437 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/6d3b5206cfaf5a85c128b671b1d9527ed553c930\/Objects\/bytearrayobject.c#L527-L575\" rel=\"noopener noreferrer nofollow\">bytearray_setslice<\/a>:<\/p>\n<pre><code class=\"cpp\">static int bytearray_setslice_linear(PyByteArrayObject *self,                           Py_ssize_t lo, Py_ssize_t hi,                           char *bytes, Py_ssize_t bytes_len) {     Py_ssize_t avail = hi - lo;     char *buf = PyByteArray_AS_STRING(self);     Py_ssize_t growth = bytes_len - avail;     int res = 0;     assert(avail &gt;= 0);      if (growth &lt; 0) {         if (!_canresize(self))             return -1;          if (lo == 0) {             \/* Shrink the buffer by advancing its logical start *\/             self-&gt;ob_start -= growth;             \/*               0   lo               hi             old_size               |   |&lt;----avail-----&gt;|&lt;-----tail------&gt;|               |      |&lt;-bytes_len-&gt;|&lt;-----tail------&gt;|               0    new_lo         new_hi          new_size             *\/         }         else {             \/*               0   lo               hi               old_size               |   |&lt;----avail-----&gt;|&lt;-----tomove------&gt;|               |   |&lt;-bytes_len-&gt;|&lt;-----tomove------&gt;|               0   lo         new_hi              new_size             *\/             memmove(buf + lo + bytes_len, buf + hi,                     Py_SIZE(self) - hi);         }         \/\/ ...     }     \/\/ ... }<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0442\u0430\u043a\u043e\u0439 \u043f\u0438\u0442\u043e\u043d \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u043a\u043e\u0434 \u0432\u044b\u0448\u0435:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; b = bytearray(b'abc') &gt;&gt;&gt; del b[0] &gt;&gt;&gt; b bytearray(b'bc') &gt;&gt;&gt; b.__alloc__() 4<\/code><\/pre>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u0442\u0435: \u043c\u044b \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u043d\u0443\u043b\u0435\u0432\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 <code>bytearray<\/code>, \u043d\u043e \u043f\u043e\u0447\u0435\u043c\u0443-\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432 <code>self-&gt;ob_alloc<\/code>. \u041e\u0442\u0432\u0435\u0442 \u0432 \u043a\u043e\u0434\u0435 \u0432\u044b\u0448\u0435 \u043d\u0430 C. \u0427\u0442\u043e \u0442\u0430\u043c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442?<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u043c\u0441\u044f \u0441 \u00ab\u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0440\u043e\u0441\u0442\u043e\u043c\u00bb \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0442\u043e \u043c\u044b \u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043e\u0442\u043a\u0443\u0434\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435: \u0441 \u043d\u0443\u043b\u044f \u0438\u043b\u0438 \u043d\u0435\u0442<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0443\u0434\u0430\u0435\u043c, \u0441\u043a\u0430\u0436\u0435\u043c <code>del b[2:4]<\/code>, \u0442\u043e \u043d\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u0434\u0442\u0438 \u043f\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u043c\u0443 \u043f\u0443\u0442\u0438 \u0438 \u0434\u0432\u0438\u0433\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>memmove<\/code><\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0441 \u043d\u0443\u043b\u044f, \u0442\u043e \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c <code>ob_start<\/code> \u043d\u0430 \u043d\u0443\u0436\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u0430\u0439\u0442<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0443\u0442 \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0433\u043e\u0432\u043e\u0440\u043a\u0443 \u043f\u0440\u043e <code>self-&gt;ob_start -= growth;<\/code> \u0422\u0443\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 C pointer math. \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u0443\u0442 \u043c\u044b \u043e\u0442\u043d\u0438\u043c\u0430\u0435\u043c \u0447\u0438\u0441\u043b\u043e \u043e\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f (\u0442\u043e\u0447\u043d\u0435\u0435 \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u0435\u043c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>growth<\/code> \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439)! \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435: <code>self-&gt;ob_start -= (growth * sizeof(char))<\/code> . \u0422\u043e \u0435\u0441\u0442\u044c, \u043c\u044b \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u043d\u0443\u0442\u0440\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c \u0432\u043d\u0443\u0442\u0440\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u041f\u0440\u0438\u043c\u0435\u0440 (<a href=\"https:\/\/www.tutorialspoint.com\/cprogramming\/c_pointer_arithmetic.htm\" rel=\"noopener noreferrer nofollow\">\u043e\u0442\u0441\u044e\u0434\u0430<\/a>):<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt;  int main() {   int int_arr[] = {12, 23, 45};   int *ptrArr = int_arr;    printf(\"Value at ptrArr: %d\\n\", *ptrArr);  \/\/ 12    \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c 2 \u043a \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044e:   ptrArr = ptrArr + 2;  \/\/ ptrArr + (2 * sizeof(int))   \/\/ \u041c\u044b \u0441 12 \u043f\u0435\u0440\u0435\u0432\u0435\u043b\u0438 \u043d\u0430 2 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u043f\u0435\u0440\u0435\u0434: \u0434\u043e 45   printf(\"Value at ptrArr after adding 2: %d\\n\", *ptrArr);  \/\/ 45    return 0; }<\/code><\/pre>\n<p>\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c <code>self-&gt;ob_start -= growth<\/code> \u043f\u0440\u0438 <code>growth = -1<\/code>, \u0431\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c: <code>self-&gt;ob_start = self.ob_start - (-1 * sizeof(char))<\/code>, \u0433\u0434\u0435 <code>sizeof(char)<\/code>  \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f 1. \u0418 \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 <code>del b[0]<\/code>  \u043d\u0430 \u043e\u0434\u0438\u043d \u0431\u0430\u0439\u0442. \u0421\u043c\u043e\u0442\u0440\u0438\u0442\u0435:<\/p>\n<pre><code class=\"cpp\">static inline char* PyByteArray_AS_STRING(PyObject *op) {     PyByteArrayObject *self = _PyByteArray_CAST(op);     if (Py_SIZE(self)) {         return self-&gt;ob_start;     }     return _PyByteArray_empty_string; }<\/code><\/pre>\n<p>\u041f\u043e\u0447\u0442\u0438 \u0432\u0435\u0437\u0434\u0435 \u0432 <code>bytearray<\/code> \u043c\u044b \u0434\u0430\u043b\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>self-&gt;ob_start<\/code> \u0447\u0435\u0440\u0435\u0437  <code>PyByteArray_AS_STRING<\/code>. \u0418 \u043f\u043e\u0442\u043e\u043c\u0443 \u0442\u0430\u043a\u0430\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0430\u0435\u0442 \u043d\u0430\u043c O(1) \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0441 \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438!<\/p>\n<h2>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 .append()<\/h2>\n<p>\u0421\u0430\u043c \u043f\u043e \u0441\u0435\u0431\u0435 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/6d3b5206cfaf5a85c128b671b1d9527ed553c930\/Objects\/bytearrayobject.c#L1774-L1801\" rel=\"noopener noreferrer nofollow\">bytearray.append<\/a> \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439. \u041d\u043e, \u0432 \u043d\u0435\u043c \u0435\u0441\u0442\u044c \u0441\u0430\u043c\u0430\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c:<\/p>\n<pre><code class=\"cpp\">if (PyByteArray_Resize((PyObject *)self, n + 1) &lt; 0)     return NULL; PyByteArray_AS_STRING(self)[n] = item;<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043d\u0435\u0439. \u041a\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/6d3b5206cfaf5a85c128b671b1d9527ed553c930\/Objects\/bytearrayobject.c#L173-L250\" rel=\"noopener noreferrer nofollow\">\u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435<\/a> <code>bytearray<\/code>? \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u0430: \u0431\u0443\u0434\u0435\u043c \u043b\u0438 \u043c\u044b \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0435\u0440\u0435-\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044e? \u0418\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0443\u0436\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u0430\u0439\u0442?<\/p>\n<pre><code class=\"cpp\">int PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) {     void *sval;     PyByteArrayObject *obj = ((PyByteArrayObject *)self);     \/* All computations are done unsigned to avoid integer overflows        (see issue #22335). *\/     size_t alloc = (size_t) obj-&gt;ob_alloc;     size_t logical_offset = (size_t) (obj-&gt;ob_start - obj-&gt;ob_bytes);     size_t size = (size_t) requested_size;      if (size + logical_offset + 1 &lt;= alloc) {         \/* Current buffer is large enough to host the requested size,            decide on a strategy. *\/         if (size &lt; alloc \/ 2) {             \/* Major downsize; resize down to exact size *\/             alloc = size + 1;         }         else {             \/* Minor downsize; quick exit *\/             Py_SET_SIZE(self, size);             PyByteArray_AS_STRING(self)[size] = '\\0'; \/* Trailing null *\/             return 0;         }     }     else {         \/* Need growing, decide on a strategy *\/         if (size &lt;= alloc * 1.125) {             \/* Moderate upsize; overallocate similar to list_resize() *\/             alloc = size + (size &gt;&gt; 3) + (size &lt; 9 ? 3 : 6);         }         else {             \/* Major upsize; resize up to exact size *\/             alloc = size + 1;         }     }     \/\/ ...   }<\/code><\/pre>\n<p>\u0427\u0442\u043e \u043c\u044b \u0432\u0438\u0434\u0438\u043c?<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f, \u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0439\u0442\u0438 \u043f\u043e \u0434\u043e\u043b\u0433\u043e\u043c\u0443 \u043f\u0443\u0442\u0438 (\u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0432 \u0434\u0432\u0430 \u0440\u0430\u0437\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e), \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c, \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0437\u0430\u043c\u0435\u043d\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c <code>\\0<\/code> \u0432 \u043a\u043e\u043d\u0435\u0446. \u0422\u0430\u043a \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u043d\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u00ab\u043b\u0438\u0448\u043d\u0435\u0439\u00bb \u043f\u0430\u043c\u044f\u0442\u0438<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043d\u0443\u0436\u043d\u0430 (\u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0432 <code>1.125<\/code> \u0440\u0430\u0437\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e), \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u043c\u044f\u0442\u0438<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u0430\u043b\u0435\u0435, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0430\u043c\u0443 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044e \/ \u0440\u0435\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044e \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 \u043f\u043e\u043b\u044f \u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u0435. \u0422\u0443\u0442 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 <code>ob_start<\/code> \u0432 <code>logical_offset<\/code>: <\/p>\n<pre><code class=\"cpp\">    \/\/ ...     if (logical_offset &gt; 0) {         sval = PyMem_Malloc(alloc);         if (sval == NULL) {             PyErr_NoMemory();             return -1;         }         memcpy(sval, PyByteArray_AS_STRING(self),                Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self)));         PyMem_Free(obj-&gt;ob_bytes);     }     else {         sval = PyMem_Realloc(obj-&gt;ob_bytes, alloc);         if (sval == NULL) {             PyErr_NoMemory();             return -1;         }     }      obj-&gt;ob_bytes = obj-&gt;ob_start = sval;     Py_SET_SIZE(self, size);     obj-&gt;ob_alloc = alloc;     obj-&gt;ob_bytes[size] = '\\0'; \/* Trailing null byte *\/      return 0; }<\/code><\/pre>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 <code>ob_start<\/code> \u0431\u043e\u043b\u044c\u0448\u0435 <code>ob_bytes<\/code>, \u0442\u043e \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u0434\u0435\u043b\u043e \u0441 \u00ab\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c\u00bb \u0441\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0433\u043b\u0430\u0432\u0435. \u0422\u043e\u0433\u0434\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043d\u043e\u0432\u044b\u0439 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <a href=\"https:\/\/docs.python.org\/3\/c-api\/memory.html#c.PyMem_Malloc\" rel=\"noopener noreferrer nofollow\">PyMem_Malloc<\/a>. \u0418 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u0432 \u043d\u043e\u0432\u043e\u0435 \u043c\u0435\u0441\u0442\u043e \u0438\u0437 <code>PyByteArray_AS_STRING(self)<\/code> (<code>self-&gt;ob_start<\/code>) \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430. \u0422\u043e \u0435\u0441\u0442\u044c, \u043d\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u0443\u0436\u0435 \u043e\u0442\u0440\u043e\u0448\u0435\u043d\u043d\u044b\u0435 \u043a\u0443\u0441\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438. \u0422\u0430\u043a\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0434\u043e\u043b\u0433\u0430\u044f<\/p>\n<\/li>\n<li>\n<p>\u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 (\u0435\u0441\u043b\u0438 \u043c\u044b \u043d\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u0438 \u043e\u0431\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442), \u0442\u043e \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <a href=\"https:\/\/docs.python.org\/3\/c-api\/memory.html#c.PyMem_Realloc\" rel=\"noopener noreferrer nofollow\">PyMem_Realloc<\/a> \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0431\u0430\u0439\u0442 \u043a <code>self-&gt;ob_bytes<\/code>, \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435<\/p>\n<\/li>\n<\/ul>\n<p>\u0418\u0442\u043e\u0433\u043e, \u0434\u0430\u043d\u043d\u043e\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441: \u043a\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u0431\u044b\u0441\u0442\u0440\u0435\u0435?<\/p>\n<pre><code class=\"python\">b1 = bytearray(b'abc') b1.append(ord(b'd')) del b1[0]  b2 = bytearray(b'abc') del b2[0] b2.append(ord(b'd'))<\/code><\/pre>\n<p>\u0414\u0430, \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e. \u041f\u0435\u0440\u0432\u044b\u0439 \u043a\u043e\u0434 \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u0417\u0430\u043c\u0435\u0440\u044b:<\/p>\n<pre><code class=\"bash\">\u00bb pyperf timeit -s 'b = bytearray(b\"abc\"); c = ord(b\"d\")' '                    b.append(c); del b[0]' ..................... Mean +- std dev: 33.1 ns +- 0.1 ns                                                                                                                                                  \u00bb pyperf timeit -s 'b = bytearray(b\"abc\"); c = ord(b\"d\")' ' del b[0]; b.append(ord(b\"d\"))' ..................... Mean +- std dev: 41.3 ns +- 0.4 ns<\/code><\/pre>\n<h2>\u0412\u044b\u0432\u043e\u0434\u044b<\/h2>\n<p>\u041c\u044b \u043e\u0431\u0441\u0443\u0434\u0438\u043b\u0438 \u043c\u043d\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e. \u041d\u043e \u043c\u043d\u043e\u0433\u043e \u0432\u0441\u0435\u0433\u043e \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0437\u0430 \u043a\u0430\u0434\u0440\u043e\u043c. \u041f\u043e\u0447\u0435\u043c\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0434 \u0432\u0438\u0434\u0430<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; b = bytearray(b'1234') &gt;&gt;&gt; del b[:4:2] &gt;&gt;&gt; b bytearray(b'24') &gt;&gt;&gt; b.__alloc__() 5<\/code><\/pre>\n<p>\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a, \u043a\u0430\u043a \u043e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442? \u041e\u0442\u0432\u0435\u0442 \u043f\u0440\u044f\u0447\u0435\u0442\u0441\u044f \u0432 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/4803cd0244847f286641c85591fda08b513cea52\/Objects\/bytearrayobject.c#L704-L742\" rel=\"noopener noreferrer nofollow\">bytearray_ass_subscript<\/a>. <\/p>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0442, \u0437\u0430\u0431\u0435\u0433\u0430\u0439\u0442\u0435 \u043a\u043e \u043c\u043d\u0435 \u0432 <a href=\"https:\/\/t.me\/opensource_findings\" rel=\"noopener noreferrer nofollow\">\u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c \u043a\u0430\u043d\u0430\u043b<\/a>. \u0422\u0430\u043c \u043c\u044b \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e \u043e\u0431\u0441\u0443\u0436\u0434\u0430\u0435\u043c \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0439. \u0421 \u043b\u044e\u0431\u043e\u0432\u044c\u044e \u0438 \u0433\u043b\u0443\u0431\u043e\u043a\u0438\u043c \u043f\u043e\u0433\u0440\u0443\u0436\u0435\u043d\u0438\u0435\u043c. \u0412 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0438 \u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0448\u0435.<\/p>\n<p>\u0410 \u0447\u0442\u043e-\u0442\u043e (\u043a\u0430\u043a, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u0435\u0441\u0442\u043e <code>bytearray<\/code> \u0441\u0440\u0435\u0434\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 \u043f\u0438\u0442\u043e\u043d\u0430 \u0438 <code>ob_exports<\/code>) \u044f \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u043b \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u043c \u0432\u0438\u0434\u0435\u043e.<\/p>\n<p>\u041d\u0443 \u0430 \u0432\u044b\u0432\u043e\u0434 \u043e\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u2013 \u0435\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442\u0435, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432\u0430\u0448 \u043a\u043e\u0434, \u0442\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u0438 \u0431\u044b\u0441\u0442\u0440\u044b\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b! \ud83d\ude42<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/861820\/\"> https:\/\/habr.com\/ru\/articles\/861820\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u041d\u0438\u043a\u0438\u0442\u0430 \u0421\u043e\u0431\u043e\u043b\u0435\u0432, \u044f core-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f CPython, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0430\u0432\u0442\u043e\u0440 \u0441\u0435\u0440\u0438\u0438 \u0432\u0438\u0434\u0435\u043e \u043f\u0440\u043e \u0435\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e.<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a <code>bytearray<\/code> \u0443\u0441\u0442\u0440\u043e\u0435\u043d \u0432\u043d\u0443\u0442\u0440\u0438.<\/p>\n<p>\u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e: \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, \u0440\u0430\u0437\u043d\u044b\u0435 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440\u044b \u0432 CPython, C pointer math, \u0434\u0435\u0442\u0430\u043b\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0442\u0430\u043a\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u0438\u043b\u0438 \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u043d\u0435\u0437\u043d\u0430\u043a\u043e\u043c\u043e\u00a0\u2014 \u0434\u043e\u0431\u0440\u043e \u043f\u043e\u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c!<\/p>\n<hr\/>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0432\u0438\u0434\u0435\u043e, \u0430 \u0434\u0430\u043b\u0435\u0435 \u0432 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u043e\u043f\u0438\u0448\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b.<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/6746ec6cfbab546b0d462778\" data-style=\"\" id=\"6746ec6cfbab546b0d462778\" width=\"\"><\/div>\n<h2>\u041a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 bytearray \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0430\u0445 CPython?<\/h2>\n<p>\u0421\u0430\u043c\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u0438\u0442\u043e\u043d\u043e\u043c \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">typedef struct {     PyObject_VAR_HEAD     Py_ssize_t ob_alloc;   \/* How many bytes allocated in ob_bytes *\/     char *ob_bytes;        \/* Physical backing buffer *\/     char *ob_start;        \/* Logical start inside ob_bytes *\/     Py_ssize_t ob_exports; \/* How many buffer exports *\/ } PyByteArrayObject;<\/code><\/pre>\n<p>\u0427\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u0435\u0441\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e?<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html#c.PyObject_VAR_HEAD\" rel=\"noopener noreferrer nofollow\">PyObject_VAR_HEAD<\/a> \u2013 \u043c\u0430\u043a\u0440\u043e\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 <code>PyVarObject ob_base;<\/code> \u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442: \u0432\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 (<code>object<\/code> \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435), \u0438 \u0447\u0442\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u0430 \u0431\u0443\u0434\u0443\u0442 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f. \u0421\u0440\u0430\u0432\u043d\u0438\u043c <code>bytearray()<\/code> \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c 0 \u0438 <code>bytearray(b'abc')<\/code> \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c 4 \u0431\u0430\u0439\u0442\u0430<\/p>\n<\/li>\n<li>\n<p><code>Py_ssize_t ob_alloc<\/code> \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u0434\u0430\u043b\u044c\u0448\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u043e\u043b\u0448\u0435\u0431\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 <code>__alloc__<\/code><\/p>\n<\/li>\n<li>\n<p><code>char *ob_bytes<\/code> \u2013 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0431\u0443\u0444\u0435\u0440 \u0441 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438<\/p>\n<\/li>\n<li>\n<p><code>char *ob_start<\/code>  \u2013 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f, \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043d\u0430\u0447\u0430\u043b\u043e \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0440\u0435\u0434\u0438 <code>ob_bytes<\/code>, \u0434\u0430\u043b\u0435\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0437\u0430\u0447\u0435\u043c \u043e\u043d\u043e \u043d\u0443\u0436\u043d\u043e<\/p>\n<\/li>\n<li>\n<p><code>Py_ssize_t ob_exports<\/code>  \u2013 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0434\u0435\u043b\u0430\u043b\u0438 &#171;export&#187; \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0447\u0435\u0440\u0435\u0437 <code>Buffer<\/code> \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u0438 \u043c\u0435\u0442\u043e\u0434 <code>__buffer__<\/code>  <\/p>\n<\/li>\n<\/ul>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 bytearray<\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u0442\u044c <code>bytearray<\/code> \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438, <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#bytearray\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e<\/a> \u043c\u044b \u0442\u0443\u0442 \u043f\u0435\u0440\u0435\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u043c. \u0410 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043c\u044b \u043d\u0430 C-API \u0444\u0443\u043d\u043a\u0446\u0438\u044e <a href=\"https:\/\/docs.python.org\/3\/c-api\/bytearray.html#c.PyByteArray_FromStringAndSize\" rel=\"noopener noreferrer nofollow\">PyByteArray_FromStringAndSize<\/a> (\u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0443, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u043d\u0435 \u043f\u0438\u0442\u043e\u043d\u043e\u0432\u0441\u043a\u0438\u0439 <code>str<\/code>, \u0430 \u0441\u0438\u0448\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435), \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f <code>bytearray<\/code> \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u043a\u043e\u0434\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/6d3b5206cfaf5a85c128b671b1d9527ed553c930\/Objects\/bytearrayobject.c#L1204-L1216\" rel=\"noopener noreferrer nofollow\">bytearray.copy<\/a>. \u0414\u043b\u044f \u043d\u0430\u0441 \u043e\u043d\u0430 \u2013 \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u0430\u044f:<\/p>\n<pre><code class=\"cpp\">PyObject * PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size) {     Py_ssize_t alloc;     \/* \u041a\u043e\u0434 \u0443\u043f\u0440\u043e\u0449\u0435\u043d \u0434\u043b\u044f \u0446\u0435\u043b\u0435\u0439 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438. *\/     PyByteArrayObject *new = PyObject_New(PyByteArrayObject, &amp;PyByteArray_Type);     if (size == 0) {         new-&gt;ob_bytes = NULL;         alloc = 0;     }     else {         alloc = size + 1;         new-&gt;ob_bytes = PyMem_Malloc(alloc);         if (new-&gt;ob_bytes == NULL) {             Py_DECREF(new);             return PyErr_NoMemory();         }         if (bytes != NULL &amp;&amp; size &gt; 0)             memcpy(new-&gt;ob_bytes, bytes, size);         new-&gt;ob_bytes[size] = '\\0';  \/* Trailing null byte *\/     }     Py_SET_SIZE(new, size);     new-&gt;ob_alloc = alloc;     new-&gt;ob_start = new-&gt;ob_bytes;     new-&gt;ob_exports = 0;     return (PyObject *)new; }<\/code><\/pre>\n<p>\u0427\u0442\u043e \u043d\u0430\u0441 \u0437\u0434\u0435\u0441\u044c \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u0442\u044c?<\/p>\n<ul>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0441 \u0434\u043b\u0438\u043d\u043d\u043e\u0439 0, \u0438\u043c\u0435\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u043f\u0443\u0442\u044c, \u043d\u0438\u0447\u0435\u0433\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u044b \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u043c<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043d\u0435 \u043d\u0443\u043b\u0435\u0432\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u00ab\u0441\u0442\u0440\u043e\u043a\u0438\u00bb, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0430\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438, \u0442\u043e \u043c\u044b \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0430 1 \u0431\u0430\u0439\u0442 \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <a href=\"https:\/\/docs.python.org\/3\/c-api\/memory.html#c.PyMem_Malloc\" rel=\"noopener noreferrer nofollow\">PyMem_Malloc<\/a>. \u041f\u043e \u0441\u0443\u0442\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043d\u0435\u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u0443\u0441\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043d\u0430\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u0430. \u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438 \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u0439 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440<\/p>\n<\/li>\n<li>\n<p>\u0414\u0430\u043b\u0435\u0435, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <a href=\"https:\/\/www.tutorialspoint.com\/c_standard_library\/c_function_memcpy.htm\" rel=\"noopener noreferrer nofollow\">memcpy<\/a> \u0434\u043b\u044f \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f <code>bytes<\/code> \u0432 <code>new-&gt;ob_bytes<\/code> \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c <code>size<\/code> (\u043d\u0430 1 \u043c\u0435\u043d\u044c\u0448\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0443\u0447\u0430\u0441\u0442\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438)<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u0448\u0430\u0433\u043e\u043c \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c <code>\\0<\/code> (null-byte) \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u043d\u0435\u0437\u0430\u043d\u044f\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432 <code>-&gt;ob_bytes<\/code><\/p>\n<\/li>\n<li>\n<p>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u043d\u0443\u0436\u043d\u044b\u0435 \u043f\u043e\u043b\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b <\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u043d\u0430\u043c \u0432\u044b\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0447\u0435\u0440\u0435\u0437 \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <code>__alloc__<\/code>:<\/p>\n<pre><code class=\"cpp\">PyDoc_STRVAR(alloc_doc, \"B.__alloc__() -&gt; int\\n\\ \\n\\ Return the number of bytes actually allocated.\");  static PyObject * bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) {     return PyLong_FromSsize_t(self-&gt;ob_alloc); }<\/code><\/pre>\n<p>\u0418 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; b = bytearray(b'abc') &gt;&gt;&gt; b.__alloc__() 4<\/code><\/pre>\n<p>\u0412\u0441\u0435 \u0432\u0435\u0440\u043d\u043e, \u043e\u0442\u0432\u0435\u0442 <code>4<\/code>\u2014 \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0431\u0430\u0439\u0442 \u2013 <code>\\0<\/code>!<\/p>\n<h2>\u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0431\u0430\u0439\u0442\u044b \u0438\u0437 \u043d\u0430\u0447\u0430\u043b\u0430<\/h2>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0442\u0435\u043c\u0430 \u2013 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0441 <code>ob_start<\/code>. \u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0431\u0430\u0439\u0442, \u043d\u0430\u043c \u0447\u0430\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0431\u0430\u0439\u0442\u044b \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u0432 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u0430\u0439\u0442. \u0422\u043e \u0435\u0441\u0442\u044c, \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c <code>del b[:4]<\/code> \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u044b\u0445 \u0447\u0435\u0442\u044b\u0440\u0435\u0445 \u0431\u0430\u0439\u0442.<\/p>\n<p>\u041a\u0430\u043a \u043e\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c? \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/6d3b5206cfaf5a85c128b671b1d9527ed553c930\/Objects\/bytearrayobject.c#L443-L467\" rel=\"noopener noreferrer nofollow\">bytearray_setslice_linear<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0437 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/6d3b5206cfaf5a85c128b671b1d9527ed553c930\/Objects\/bytearrayobject.c#L527-L575\" rel=\"noopener noreferrer nofollow\">bytearray_setslice<\/a>:<\/p>\n<pre><code class=\"cpp\">static int bytearray_setslice_linear(PyByteArrayObject *self,                           Py_ssize_t lo, Py_ssize_t hi,                           char *bytes, Py_ssize_t bytes_len) {     Py_ssize_t avail = hi - lo;     char *buf = PyByteArray_AS_STRING(self);     Py_ssize_t growth = bytes_len - avail;     int res = 0;     assert(avail &gt;= 0);      if (growth &lt; 0) {         if (!_canresize(self))             return -1;          if (lo == 0) {             \/* Shrink the buffer by advancing its logical start *\/             self-&gt;ob_start -= growth;             \/*               0   lo               hi             old_size               |   |&lt;----avail-----&gt;|&lt;-----tail------&gt;|               |      |&lt;-bytes_len-&gt;|&lt;-----tail------&gt;|               0    new_lo         new_hi          new_size             *\/         }         else {             \/*               0   lo               hi               old_size               |   |&lt;----avail-----&gt;|&lt;-----tomove------&gt;|               |   |&lt;-bytes_len-&gt;|&lt;-----tomove------&gt;|               0   lo         new_hi              new_size             *\/             memmove(buf + lo + bytes_len, buf + hi,                     Py_SIZE(self) - hi);         }         \/\/ ...     }     \/\/ ... }<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0442\u0430\u043a\u043e\u0439 \u043f\u0438\u0442\u043e\u043d \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u043a\u043e\u0434 \u0432\u044b\u0448\u0435:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; b = bytearray(b'abc') &gt;&gt;&gt; del b[0] &gt;&gt;&gt; b bytearray(b'bc') &gt;&gt;&gt; b.__alloc__() 4<\/code><\/pre>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u0442\u0435: \u043c\u044b \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u043d\u0443\u043b\u0435\u0432\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 <code>bytearray<\/code>, \u043d\u043e \u043f\u043e\u0447\u0435\u043c\u0443-\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432 <code>self-&gt;ob_alloc<\/code>. \u041e\u0442\u0432\u0435\u0442 \u0432 \u043a\u043e\u0434\u0435 \u0432\u044b\u0448\u0435 \u043d\u0430 C. \u0427\u0442\u043e \u0442\u0430\u043c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442?<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u043c\u0441\u044f \u0441 \u00ab\u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0440\u043e\u0441\u0442\u043e\u043c\u00bb \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0442\u043e \u043c\u044b \u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043e\u0442\u043a\u0443\u0434\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435: \u0441 \u043d\u0443\u043b\u044f \u0438\u043b\u0438 \u043d\u0435\u0442<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0443\u0434\u0430\u0435\u043c, \u0441\u043a\u0430\u0436\u0435\u043c <code>del b[2:4]<\/code>, \u0442\u043e \u043d\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u0434\u0442\u0438 \u043f\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u043c\u0443 \u043f\u0443\u0442\u0438 \u0438 \u0434\u0432\u0438\u0433\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>memmove<\/code><\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0441 \u043d\u0443\u043b\u044f, \u0442\u043e \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c <code>ob_start<\/code> \u043d\u0430 \u043d\u0443\u0436\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u0430\u0439\u0442<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0443\u0442 \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0433\u043e\u0432\u043e\u0440\u043a\u0443 \u043f\u0440\u043e <code>self-&gt;ob_start -= growth;<\/code> \u0422\u0443\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 C pointer math. \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u0443\u0442 \u043c\u044b \u043e\u0442\u043d\u0438\u043c\u0430\u0435\u043c \u0447\u0438\u0441\u043b\u043e \u043e\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f (\u0442\u043e\u0447\u043d\u0435\u0435 \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u0435\u043c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>growth<\/code> \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439)! \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435: <code>self-&gt;ob_start -= (growth * sizeof(char))<\/code> . \u0422\u043e \u0435\u0441\u0442\u044c, \u043c\u044b \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u043d\u0443\u0442\u0440\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c \u0432\u043d\u0443\u0442\u0440\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u041f\u0440\u0438\u043c\u0435\u0440 (<a href=\"https:\/\/www.tutorialspoint.com\/cprogramming\/c_pointer_arithmetic.htm\" rel=\"noopener noreferrer nofollow\">\u043e\u0442\u0441\u044e\u0434\u0430<\/a>):<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt;  int main() {   int int_arr[] = {12, 23, 45};   int *ptrArr = int_arr;    printf(\"Value at ptrArr: %d\\n\", *ptrArr);  \/\/ 12    \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c 2 \u043a \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044e:   ptrArr = ptrArr + 2;  \/\/ ptrArr + (2 * sizeof(int))   \/\/ \u041c\u044b \u0441 12 \u043f\u0435\u0440\u0435\u0432\u0435\u043b\u0438 \u043d\u0430 2 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u043f\u0435\u0440\u0435\u0434: \u0434\u043e 45   printf(\"Value at ptrArr after adding 2: %d\\n\", *ptrArr);  \/\/ 45    return 0; }<\/code><\/pre>\n<p>\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c <code>self-&gt;ob_start -= growth<\/code> \u043f\u0440\u0438 <code>growth = -1<\/code>, \u0431\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c: <code>self-&gt;ob_start = self.ob_start - (-1 * sizeof(char))<\/code>, \u0433\u0434\u0435 <code>sizeof(char)<\/code>  \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f 1. \u0418 \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 <code>del b[0]<\/code>  \u043d\u0430 \u043e\u0434\u0438\u043d \u0431\u0430\u0439\u0442. \u0421\u043c\u043e\u0442\u0440\u0438\u0442\u0435:<\/p>\n<pre><code class=\"cpp\">static inline char* PyByteArray_AS_STRING(PyObject *op) {     PyByteArrayObject *self = _PyByteArray_CAST(op);     if (Py_SIZE(self)) {         return self-&gt;ob_start;     }     return _PyByteArray_empty_string; }<\/code><\/pre>\n<p>\u041f\u043e\u0447\u0442\u0438 \u0432\u0435\u0437\u0434\u0435 \u0432 <code>bytearray<\/code> \u043c\u044b \u0434\u0430\u043b\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>self-&gt;ob_start<\/code> \u0447\u0435\u0440\u0435\u0437  <code>PyByteArray_AS_STRING<\/code>. \u0418 \u043f\u043e\u0442\u043e\u043c\u0443 \u0442\u0430\u043a\u0430\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0430\u0435\u0442 \u043d\u0430\u043c O(1) \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0441 \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438!<\/p>\n<h2>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 .append()<\/h2>\n<p>\u0421\u0430\u043c \u043f\u043e \u0441\u0435\u0431\u0435 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/6d3b5206cfaf5a85c128b671b1d9527ed553c930\/Objects\/bytearrayobject.c#L1774-L1801\" rel=\"noopener noreferrer nofollow\">bytearray.append<\/a> \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439. \u041d\u043e, \u0432 \u043d\u0435\u043c \u0435\u0441\u0442\u044c \u0441\u0430\u043c\u0430\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c:<\/p>\n<pre><code class=\"cpp\">if (PyByteArray_Resize((PyObject *)self, n + 1) &lt; 0)     return NULL; PyByteArray_AS_STRING(self)[n] = item;<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043d\u0435\u0439. \u041a\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/6d3b5206cfaf5a85c128b671b1d9527ed553c930\/Objects\/bytearrayobject.c#L173-L250\" rel=\"noopener noreferrer nofollow\">\u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435<\/a> <code>bytearray<\/code>? \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u0430: \u0431\u0443\u0434\u0435\u043c \u043b\u0438 \u043c\u044b \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0435\u0440\u0435-\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044e? \u0418\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0443\u0436\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u0430\u0439\u0442?<\/p>\n<pre><code class=\"cpp\">int PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) {     void *sval;     PyByteArrayObject *obj = ((PyByteArrayObject *)self);     \/* All computations are done unsigned to avoid integer overflows        (see issue #22335). *\/     size_t alloc = (size_t) obj-&gt;ob_alloc;     size_t logical_offset = (size_t) (obj-&gt;ob_start - obj-&gt;ob_bytes);     size_t size = (size_t) requested_size;      if (size + logical_offset + 1 &lt;= alloc) {         \/* Current buffer is large enough to host the requested size,            decide on a strategy. *\/         if (size &lt; alloc \/ 2) {             \/* Major downsize; resize down to exact size *\/             alloc = size + 1;         }         else {             \/* Minor downsize; quick exit *\/             Py_SET_SIZE(self, size);             PyByteArray_AS_STRING(self)[size] = '\\0'; \/* Trailing null *\/             return 0;         }     }     else {         \/* Need growing, decide on a strategy *\/         if (size &lt;= alloc * 1.125) {             \/* Moderate upsize; overallocate similar to list_resize() *\/             alloc = size + (size &gt;&gt; 3) + (size &lt; 9 ? 3 : 6);         }         else {             \/* Major upsize; resize up to exact size *\/             alloc = size + 1;         }     }     \/\/ ...   }<\/code><\/pre>\n<p>\u0427\u0442\u043e \u043c\u044b \u0432\u0438\u0434\u0438\u043c?<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f, \u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0439\u0442\u0438 \u043f\u043e \u0434\u043e\u043b\u0433\u043e\u043c\u0443 \u043f\u0443\u0442\u0438 (\u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0432 \u0434\u0432\u0430 \u0440\u0430\u0437\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e), \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c, \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0437\u0430\u043c\u0435\u043d\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c <code>\\0<\/code> \u0432 \u043a\u043e\u043d\u0435\u0446. \u0422\u0430\u043a \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u043d\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u00ab\u043b\u0438\u0448\u043d\u0435\u0439\u00bb \u043f\u0430\u043c\u044f\u0442\u0438<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043d\u0443\u0436\u043d\u0430 (\u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0432 <code>1.125<\/code> \u0440\u0430\u0437\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e), \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u043c\u044f\u0442\u0438<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u0430\u043b\u0435\u0435, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0430\u043c\u0443 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044e \/ \u0440\u0435\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044e \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 \u043f\u043e\u043b\u044f \u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u0435. \u0422\u0443\u0442 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 <code>ob_start<\/code> \u0432 <code>logical_offset<\/code>: <\/p>\n<pre><code class=\"cpp\">    \/\/ ...     if (logical_offset &gt; 0) {         sval = PyMem_Malloc(alloc);         if (sval == NULL) {        <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-440129","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/440129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=440129"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/440129\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=440129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=440129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=440129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}