{"id":33718,"date":"2023-02-12T21:51:51","date_gmt":"2023-02-12T16:21:51","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/"},"modified":"2023-02-12T21:51:51","modified_gmt":"2023-02-12T16:21:51","slug":"solved-changing-vector-in-function-by-pointer","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/","title":{"rendered":"[Solved] Changing vector in function by pointer"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-49557287\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"49557287\" data-parentid=\"49556895\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The function does not make sense.<\/p>\n<p>For starters it is unclear why you are using a pointer to the vector instead of a reference to the vector.<\/p>\n<p>Nevertheles, this declaration<\/p>\n<pre><code>vector&lt;unsigned char&gt; vec(5);\n<\/code><\/pre>\n<p>does not reseeve a memory for the vector. It initializes the vector with 5 zero-characters which will be appended with other characters in the function.<\/p>\n<p>The expression<\/p>\n<pre><code>&amp;(*result).back()\n<\/code><\/pre>\n<p>returns reference to the last element of the vector.  And this call<\/p>\n<pre><code>memcpy(&amp;(*result).back(), elem, elem_size);\n<\/code><\/pre>\n<p>will try to overwrite memory that does not belong to the vector. As result the function has undefined behaviour.<\/p>\n<p>Youy can imagine the situation the following way<\/p>\n<pre><code>   vector\n|0|0|0|0|0|\n        |a|b|c|d|\n         string\n<\/code><\/pre>\n<p>so as it is seen there is an attempt to copy characters &#8216;b&#8217;, &#8216;c&#8217;, and &#8216;d&#8217; to the memory that does not belong to the vector.<\/p>\n<p>You should use methods of the class template <code>std::vector<\/code> to append new elements.<\/p>\n<p>The function can look the following way as it is shown in the demonstrative program (provided that you want to use a pointer to vector).<\/p>\n<pre><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;string&gt;\n\nvoid addElem( std::vector&lt;unsigned char&gt; *result, const std::string &amp;str_to_add ) \n{\n    result-&gt;reserve( result-&gt;size() + str_to_add.size() );\n    result-&gt;insert( result-&gt;end(), str_to_add.begin(), str_to_add.end() );\n}   \n\nint main() \n{\n    std::vector&lt;unsigned char&gt; vec;\n    std::vector&lt;unsigned char&gt; *vec_ptr = &amp;vec;\n\n    addElem( vec_ptr, \"abcd\" );\n\n    for ( auto c : *vec_ptr ) std::cout &lt;&lt; c;\n    std::cout &lt;&lt; std::endl;\n\n    return 0;\n}\n<\/code><\/pre>\n<p>Its output is<\/p>\n<pre><code>abcd\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Changing vector in function by pointer <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The function does not make sense. For starters it is unclear why you are using a pointer to the vector instead of a reference to the vector. Nevertheles, this declaration vector&lt;unsigned char&gt; vec(5); does not reseeve a memory for the vector. It initializes the vector with 5 zero-characters which will be appended with other &#8230; <a title=\"[Solved] Changing vector in function by pointer\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/\" aria-label=\"More on [Solved] Changing vector in function by pointer\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[1537,324,712,362,806],"class_list":["post-33718","post","type-post","status-publish","format-standard","hentry","category-solved","tag-append","tag-c","tag-pointers","tag-string","tag-vector"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Changing vector in function by pointer - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Changing vector in function by pointer - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The function does not make sense. For starters it is unclear why you are using a pointer to the vector instead of a reference to the vector. Nevertheles, this declaration vector&lt;unsigned char&gt; vec(5); does not reseeve a memory for the vector. It initializes the vector with 5 zero-characters which will be appended with other ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-12T16:21:51+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-changing-vector-in-function-by-pointer\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-changing-vector-in-function-by-pointer\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Changing vector in function by pointer\",\"datePublished\":\"2023-02-12T16:21:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-changing-vector-in-function-by-pointer\\\/\"},\"wordCount\":177,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"append\",\"c++\",\"pointers\",\"string\",\"vector\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-changing-vector-in-function-by-pointer\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-changing-vector-in-function-by-pointer\\\/\",\"name\":\"[Solved] Changing vector in function by pointer - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2023-02-12T16:21:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-changing-vector-in-function-by-pointer\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-changing-vector-in-function-by-pointer\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-changing-vector-in-function-by-pointer\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Changing vector in function by pointer\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Changing vector in function by pointer - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Changing vector in function by pointer - JassWeb","og_description":"[ad_1] The function does not make sense. For starters it is unclear why you are using a pointer to the vector instead of a reference to the vector. Nevertheles, this declaration vector&lt;unsigned char&gt; vec(5); does not reseeve a memory for the vector. It initializes the vector with 5 zero-characters which will be appended with other ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/","og_site_name":"JassWeb","article_published_time":"2023-02-12T16:21:51+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Changing vector in function by pointer","datePublished":"2023-02-12T16:21:51+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/"},"wordCount":177,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["append","c++","pointers","string","vector"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/","url":"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/","name":"[Solved] Changing vector in function by pointer - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-02-12T16:21:51+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-changing-vector-in-function-by-pointer\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Changing vector in function by pointer"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=33718"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33718\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=33718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=33718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=33718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}