{"id":2734,"date":"2022-08-30T15:27:20","date_gmt":"2022-08-30T15:27:20","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/08\/program-crashes-when-leaving-a-c-functionwhat-do-you-think-it-is-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:27:20","modified_gmt":"2022-08-30T15:27:20","slug":"program-crashes-when-leaving-a-c-functionwhat-do-you-think-it-is-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/program-crashes-when-leaving-a-c-functionwhat-do-you-think-it-is-collection-of-common-programming-errors\/","title":{"rendered":"Program crashes when leaving a c++ function&hellip;What do you think it is?-Collection of common programming errors"},"content":{"rendered":"<p>I have a c++ code, I use MSC9 to compile it. It keeps crashing randomly. For example it crashes if it is called from Perl using &#8220; but it does not crash when It is called from command line or from Ultimate++.<\/p>\n<p>I mean calling it from perl eg. <code>f.exe arg1 arg2 arg3<\/code><\/p>\n<p>Stack trace does not show much. Tracing the program line by line proved that the program fails at the end when returning&#8230;<\/p>\n<p>So it is like that<\/p>\n<pre><code>int funcname()\n{\n    return 0; 10)\n      {\n        defsize = s;\n      }\n      m_rgArray = new value_type[defsize];\n      allocated = defsize;\n      fill(0);\n    }\n      pvector(pvector const&amp; pv)\n    : m_rgArray(new value_type[pv.allocated]),\n    m_nIndex(pv.m_nIndex),allocated(pv.allocated)\n    {\n     std::copy(pv.m_rgArray, pv.m_rgArray + pv.allocated, m_rgArray);     \n    }\n\n    pvector operator=(const pvector &amp;pv)\n    {\n    m_rgArray=new value_type[pv.allocated];\n    m_nIndex=pv.m_nIndex;\n    allocated=pv.allocated;\n    std::copy(pv.m_rgArray, pv.m_rgArray + pv.allocated, m_rgArray);  \n    return *this;\n    }\n    void clear()\n    {\n       m_nIndex=0; \n       fill(allocated);    \n    }\n\n    ~pvector() {\n     delete []m_rgArray; \n    }\n\n    size_type size() const\n    { \/\/ return length of sequence\n      return m_nIndex;\n    }\n\n    size_type max_size() const\n    { \/\/ return maximum possible length of sequence\n      return 0;\n    }\n\n    void fill(size_type si)\n    {\n      for (size_type i = si;i 0 ? false : true);\n    }\n\n    iterator begin()\n    { \/\/ return iterator for beginning of mutable sequence\n      return iterator(&amp;m_rgArray[0]);\n    }\n\n    const_iterator begin() const\n    {\n      return const_iterator(&amp;m_rgArray[0]); \n    }\n\n    iterator end()\n    { \/\/ return iterator for end of mutable sequence\n      return iterator(&amp;m_rgArray[m_nIndex]);\n    }\n\n    const_iterator end() const\n    {\n      return const_iterator(&amp;m_rgArray[m_nIndex]);\n    }\n    reference operator[](size_type i)\n    {\n      if (m_nIndex&gt;i)\n      {\n        return m_rgArray[i];\n      }\n      else if (i &gt;= allocated)\n        {\n          resize(i * 2);\n        }\n        m_nIndex = i + 1;\n      return m_rgArray[i];\n    } \n    void resize(size_type s)\n    {\n      value_type * m_rgArray2;\n      size_type old_allocated = allocated;\n      allocated = s;\n      m_rgArray2 = new value_type[allocated];\n        \/\/if (allocated&gt;m_nIndex)\n        \/\/{\n        \/\/ m_nIndex=allocated;\n       \/\/ }\n       \/\/ cout  std::vector::capacity())\n        {\n            std::vector::reserve(need * 2);\n            std::vector::resize(need);\n        }\n        else if(need &gt; std::vector::size())\n        {\n            std::vector::resize(need);\n        }\n        return std::vector::at(n);\n    }\n\n    reference operator[](size_type n)\n    {\n        return at(n);\n    }\n};\n<\/code><\/pre>\n<p>I tested it with GCC 4.1.2 on Linux, so hopefully it&#8217;ll compile on Windows too.<\/p>\n<p>Edit: Here&#8217;s a new version that doesn&#8217;t inherit, since it seems experts agree it&#8217;s not a good idea (I learned something new, yay). You can implement the rest of the methods you want and just pass-through to m_vector:<\/p>\n<pre><code>template\nclass pvector\n{\npublic:\n    typedef StoredType&amp; reference;\n    typedef int size_type;\n\n    reference at(size_type n)\n    {\n        int need = n+1;\n        if(need &gt;= m_vector.capacity())\n        {\n            m_vector.reserve(need * 2);\n            m_vector.resize(need);\n        }\n        else if(need &gt;= m_vector.size())\n        {\n            m_vector.resize(need);\n        }\n        return m_vector.at(n);\n    }\n\n    reference operator[](size_type n)\n    {\n        return at(n);\n    }\n\n    size_type capacity() { return m_vector.capacity(); }\n    size_type size() { return m_vector.size(); }\n\nprivate:\n    std::vector m_vector;\n};\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2014-02-08 15:44:24. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a c++ code, I use MSC9 to compile it. It keeps crashing randomly. For example it crashes if it is called from Perl using &#8220; but it does not crash when It is called from command line or from Ultimate++. I mean calling it from perl eg. f.exe arg1 arg2 arg3 Stack trace [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2734","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2734"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2734\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}