{"id":45,"date":"2012-04-13T12:01:37","date_gmt":"2012-04-13T04:01:37","guid":{"rendered":"http:\/\/fishinbox.tk\/?p=45"},"modified":"2012-04-13T12:01:37","modified_gmt":"2012-04-13T04:01:37","slug":"dynamic-pointer-and-dynamic-memory-allocation-in-c","status":"publish","type":"post","link":"https:\/\/zhuangzr.me\/wordpress\/2012\/04\/dynamic-pointer-and-dynamic-memory-allocation-in-c\/","title":{"rendered":"Dynamic Pointer and Dynamic Memory Allocation in C++"},"content":{"rendered":"<p>Days ago a mate asked me a favor in his code and I got trouble in dynamic pointer and dynamic memory allocation.<br \/>\nWell, one-dimensional pointer is easy to use, and you can easily expand it&#8217;s occupied memory size by using realloc() as following:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;iostream&gt;\nusing namespace std;\nint n=2,* pint;\nvoid Initialization()\n{\n\tpint=new int&#x5B;n];\n}\nvoid DoSomething(){}\nint main()\n{\n\tInitialization();\n\tDoSomething();\n\tpint=(int*)realloc(pint,(++n)*sizeof(int));\n\tpint&#x5B;n-1]=123456;\n\tcout&lt;&lt;pint&#x5B;n-1]&lt;&lt;endl;\n\treturn 0;\n}\n&lt;\/pre&gt;\nBut when it comes to the two-dimensional pointer, it nolonger works if you write some code like this:\n&lt;pre lang=&quot;cpp&quot; line=&quot;1&quot; file=&quot;code2.cpp&quot; colla=&quot;+&quot;&gt;\n#include &lt;iostream&gt;\nusing namespace std;\nint n=2,len=20;\nchar** ppchar;\nvoid Initialization(){\n\tppchar=new char*&#x5B;n];\n\tfor(int i=0;i&lt;n;i++){\n\t\tppchar&#x5B;i]=new char&#x5B;len];\n\t}\n}\nvoid DoSomething(){}\nint main()\n{\n\tInitialization();\n\tDoSomething();\n\tppchar=(char**)realloc(ppchar,(++n)*len*sizeof(char));\n\tppchar&#x5B;n-1]=&quot;this is a new one&quot;;\n\tcout&lt;&lt;ppchar&#x5B;n-1]&lt;&lt;endl;\n\treturn 0;\n}\n<\/pre>\n<p>That is because you failed to notify the system, or in other words declare the address of ppchar[n-1].<br \/>\nAnd here is a correct version of the same function by adding a statement between line 16 and 17:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nppchar&#x5B;n-1]=new char&#x5B;len];\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Days ago a mate asked me a favor in his code and I got trouble in dynamic pointer and dynamic memory allocation. Well, one-dimensional pointer is easy to use, and you can easily expand it&#8217;s occupied memory size by using realloc() as following: #include &lt;iostream&gt; using namespace std; int n=2,* pint; void Initialization() { pint=new &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/zhuangzr.me\/wordpress\/2012\/04\/dynamic-pointer-and-dynamic-memory-allocation-in-c\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Dynamic Pointer and Dynamic Memory Allocation in C++&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[25,41,44,63],"class_list":["post-45","post","type-post","status-publish","format-standard","hentry","category-cpp-programming","tag-c","tag-memory-allocation","tag-pointer","tag-programming"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":0,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}