{"id":3739,"date":"2014-03-30T05:05:14","date_gmt":"2014-03-30T05:05:14","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/operator-new-allocates-only-one-element-regardless-of-how-many-are-requested-c-duplicate-collection-of-common-programming-errors\/"},"modified":"2014-03-30T05:05:14","modified_gmt":"2014-03-30T05:05:14","slug":"operator-new-allocates-only-one-element-regardless-of-how-many-are-requested-c-duplicate-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/operator-new-allocates-only-one-element-regardless-of-how-many-are-requested-c-duplicate-collection-of-common-programming-errors\/","title":{"rendered":"operator new[] allocates only one element regardless of how many are requested (C++) [duplicate]-Collection of common programming errors"},"content":{"rendered":"<p>For an introductory C++ course assignment (due tonight, of course!), I have to implement my own Vector class. Most everything seems to be working, except that I noticed in VS2012&#8217;s debugger that it appears that only one element in _arr[] actually is being allocated. Regardless of the number (n) of elements requested, it only allocates a single element in the array. I followed the debug trace, and new[] is receiving 20 for the request (the requested 5 elements * 4 bytes for an int), but when I check sizeof(_arr) later, it only shows 4 bytes. Where did the other 16 bytes end up? Why are the other 4 elements missing? No errors are indicated, and no exceptions are thrown.<\/p>\n<pre><code>template \nvoid Vector::Add ( const T&amp; val )\n{\n    \/\/ Check if a new element would grow beyond the current allocation.\n    if ( _length + 1 &gt; _allocSize )\n    {\n        size_t n = 5; \/\/  0 )\n                for( size_t idx = 0; idx &lt; _length; idx++ )\n                    newArr[idx] = _arr[idx];\n            \/\/ Delete any dynamic memory allocated to the old array.\n            delete[] _arr;\n\n            \/\/ Note: _sizeof(newArr) here indicates only 4 bytes!!!\n\n            \/\/ Point _arr to the new array\n            _arr = newArr;\n\n            \/\/ Update the allocated size to the new array size\n            _allocSize = n;\n        }\n        catch ( VectorException &amp;cException )\n        {\n            cerr<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For an introductory C++ course assignment (due tonight, of course!), I have to implement my own Vector class. Most everything seems to be working, except that I noticed in VS2012&#8217;s debugger that it appears that only one element in _arr[] actually is being allocated. Regardless of the number (n) of elements requested, it only allocates [&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-3739","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3739","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=3739"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3739\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}