title: What is the difference in execution time for allocating to the heap vs the stack?
body: <p>I am a little stuck on this question in my homework: 
  <em>Write three functions in C or C++: one that declares a large array statically, one that declares the same large array on the stack, and one that creates the same large array from the heap. Call each of the subprograms a large number of times {at least 100,000) and output the time required by each. Explain the results.</em></p>
  
  <pre><code>    int main(void)  
  {
      int staticIntArray[ARRAY_SIZE];//array on the stack
      int *ptrArray = new int[ARRAY_SIZE]; // pointer on the stack but array on the heap.
      double timeItTakes;
      clock_t tStart = clock();
      fillWithRandomNumbers(staticIntArray, ARRAY_SIZE);
      double time = static_cast&lt;double&gt;(clock() - static_cast&lt;double&gt;(tStart)/static_cast&lt;double&gt;(CLOCKS_PER_SEC));
      printf ("Array on stack time is %.10f\n", time);
      clock_t tStart2 = clock();
      fillWithRandomNumbers(ptrArray, ARRAY_SIZE);
      double time2 = static_cast&lt;double&gt;(clock() - static_cast&lt;double&gt;(tStart2)/static_cast&lt;double&gt;(CLOCKS_PER_SEC));
      printf ("Array on heap time is %.10f\n", time2);
      //cout &lt;&lt; "Array on the heap time is " &lt;&lt; (timeIntStack - time(NULL));
  }
  void fillWithRandomNumbers(int intArray[], int size)
  {
      for(int i = 0; i&lt;size; i++)
          intArray[i] = rand();
  }
  </code></pre>
  
  <p>The output is: </p>
  
  <pre><code>Array on stack time is 1.9990000000
  Array on heap time is 2.9980000000
  Press any key to continue . . .
  </code></pre>
  
  <p>What I understand is that the stack is much smaller allocation of memory that is for local variables and parameters while the heap is a large pool of dynamically allocated memory. Here are my questions...
  Does using the random class affect the time it takes for the function to execute? Is allocating large arrays on the stack slower because there is less memory available?</p>
  
  <p>I am not trying ask you to do my homework but I just need a little help clarifying the concepts... Any help would be much appreciated...</p>