Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your <code>for</code> loop:</p> <pre><code>for(int i=0;i&lt;6*p;i++) </code></pre> <p>Treats the array size as <code>6*p</code> where in the example <code>p==9</code>. So your loop treats the array size as 54.</p> <p>But array <code>a</code> has only 18 elements so your initialisation is overflowing this array.</p> <p>Array <code>b</code> has only 36 elements so your initialisation is overflowing this array as well.</p> <p>Overflowing an array will write to memory that you probably do not intend to write to, which can cause all sorts of bugs...</p> <p>You would be better to use a function like. Also do you need to <strong>NULL-terminate</strong> your strings?</p> <pre><code>void initialize(char a[], size_t aSize, char b[], size_t bSize) { int i = 0; for(; i &lt; aSize-1; i++) a[i]='-'; for(i = 0; i &lt; bSize-1; i++) b[i]='-'; // I think you need to NULL terminate.... a[aSize-1] = '\0'; b[bSize-1] = '\0'; } int main() { int p=9; char a[2*p],b[4*p]; initialize(a, sizeof(a), b, sizeof(b)); std::cout &lt;&lt; a &lt;&lt; std::endl &lt;&lt; b &lt;&lt; std::endl; return 0; } </code></pre> <p>Or even more generically in you <code>main()</code> function if you ever use arrays that are <em>not</em> of type <code>char</code>...</p> <pre><code>initialize(a, sizeof(a)/sizeof(a[0]), b, sizeof(b)/sizeof(b[0])); </code></pre> <p>Or even, to initialise the arrays just use <a href="http://www.cplusplus.com/reference/cstring/memset/" rel="nofollow"> <strong><em>memset()</em></strong></a> and then you won't need the function <code>initialize()</code> :) </p> <p>E.g.</p> <pre><code>#include &lt;string.h&gt; int main() { int p=9; char a[2*p],b[4*p]; memset(a, '-', sizeof(a)-1); a[sizeof(a)-1] == '\0'; memset(b, '-', sizeof(b)-1); b[sizeof(b)-1] == '\0'; std::cout &lt;&lt; a &lt;&lt; std::endl &lt;&lt; b &lt;&lt; std::endl; return 0; } </code></pre> <p>The other thing I just though of was "are you <strong>NULL-terminating</strong> your strings? I've changed the above examples to cater for that, otherwise when printing the string the print function won't know where to stop!</p> <p>Yet another option might also be <a href="http://www.cplusplus.com/reference/algorithm/fill/" rel="nofollow"><strong><em>std::fill()</em></strong></a> or <a href="http://www.cplusplus.com/reference/algorithm/fill_n/" rel="nofollow"><strong>*std::fill_n()*</strong></a>:</p> <pre><code>#include &lt;iostream&gt; #include &lt;algorithm&gt; // std::fill int main(int argc, char* argv[]) { int p=9; char a[2*p],b[4*p]; std::fill_n(a, sizeof(a)-1, '-'); a[sizeof(a)-1] = '\0'; std::fill_n(b, sizeof(b)-1, '-'); b[sizeof(b)-1] = '\0'; std::cout &lt;&lt; a &lt;&lt; std::endl &lt;&lt; b &lt;&lt; std::endl; return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload