{"id":3894,"date":"2014-03-30T06:17:15","date_gmt":"2014-03-30T06:17:15","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/easy-c-question-regarding-valarray-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:17:15","modified_gmt":"2014-03-30T06:17:15","slug":"easy-c-question-regarding-valarray-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/easy-c-question-regarding-valarray-collection-of-common-programming-errors\/","title":{"rendered":"Easy c++ question regarding valarray-Collection of common programming errors"},"content":{"rendered":"<p>The line<\/p>\n<pre><code>valarray data;\n<\/code><\/pre>\n<p>is automatically creating a <code>valarray<\/code> object on the stack and is calling the no-argument constructor.<\/p>\n<p>The line<\/p>\n<pre><code>data = valarray (x);\n<\/code><\/pre>\n<p>translates to<\/p>\n<pre><code>data.operator=(valarray (x));\n<\/code><\/pre>\n<p>which calls the valarray constructor that takes an int. What ends up happening is that another object is automatically created on the stack and is then assigned to the first object.<\/p>\n<p>Note that it would be better to do<\/p>\n<pre><code>valarray data(x);\n<\/code><\/pre>\n<p>so that you only create one object instead of creating two and throwing one away.<\/p>\n<p><strong>Edited to address other OP comments<\/strong><\/p>\n<p><code>x<\/code> is <strong>not<\/strong> unknown. The <code>cin &gt;&gt; x;<\/code> line in your program is setting <code>x<\/code>. So when the <code>valarray<\/code> constructor is called it is being passed an actual value. At the bottom it&#8217;s no different than this (though of course since the memory allocation is internal to <code>valarray<\/code> it can make sure to <code>delete<\/code> automatically it when the object goes away):<\/p>\n<pre><code>int* makeArray(int size) {\n    return new int[size];\n}\n\nint main() {\n    int s;\n    cin &gt;&gt; s;\n\n    int* theArray = makeArray(s);\n    \/\/ do stuff\n\n    return 0;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The line valarray data; is automatically creating a valarray object on the stack and is calling the no-argument constructor. The line data = valarray (x); translates to data.operator=(valarray (x)); which calls the valarray constructor that takes an int. What ends up happening is that another object is automatically created on the stack and is then [&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-3894","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3894","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=3894"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3894\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}