locked
testing for large arrays- stackoverflow exception RRS feed

  • Question

  • I have the following code that returns missing number from 1 to 1,000,000.

    int findMissing(unsigned int * arr, int size) { //calculate total size int n = arr[size - 1]; unsigned int total_sum = (n*(n + 1)) / 2; //subtract every element from total sum for (int i = 0; i < size; i++) { total_sum -= arr[i]; } return total_sum; }

    I'm trying to write a test case for this, I need to send an array that contains a million numbers from 1 to 1000000 to test this. But when I try to create an array of this size, I get stack overflow exception.

    Below is the code that throws the exception

    //main func

    int main()
    {

    double  a[999999];

    it works fine for smaller numbers.


    Tuesday, July 12, 2016 10:14 PM

Answers

  • Hi askatral,

    thanks for posting here.

    >>I'm trying to write a test case for this, I need to send an array that contains a million numbers from 1 to 1000000 to test this. But when I try to create an array of this size, I get stack overflow exception.
    Below is the code that throws the exception

    Try this code, use new operator:

    unsigned int *a = new unsigned int[999999];

    Best Regards,

    Sera Yu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    • Proposed as answer by Baron Bi Friday, July 29, 2016 6:18 AM
    • Marked as answer by Hart Wang Friday, July 29, 2016 8:13 AM
    Wednesday, July 13, 2016 2:30 AM

All replies

  • Default stack size is 1MB. Increase the stack size.  See /F (Set Stack Size)

    Or dynamically allocate the memory for your array.

    • Edited by RLWA32 Tuesday, July 12, 2016 10:19 PM
    Tuesday, July 12, 2016 10:18 PM
  • Hello,

     If you do as RLWA32 suggest then you should not have Stack errors.

    As a side note, remember allocation is the total amount you want to

    reserve for use.  So, dont get confused with zero-base indexing with

    total allocations. 

      Example:  long a[10] is index from 0 to 9. Using a[10] returns Out-of-Range error.

     In your declared allocation, double a[ 999999 ] is not 1 million but actually less.

    Meaning that your programmatic range index is 0 to 999998.

     Also review the memory sizes for each object types.  Like Byte is 1, Short is 2,

    Int is 4, etc.  Double is 8 bytes, I think, and Floats are 14-15 bytes of memory.

    So, allocation of 1 million Double object is equal to 8 megabytes of RAM.

     Just added info. :)


    • Edited by User3DX Wednesday, July 13, 2016 12:23 AM correct/added
    Wednesday, July 13, 2016 12:17 AM
  • Hi askatral,

    thanks for posting here.

    >>I'm trying to write a test case for this, I need to send an array that contains a million numbers from 1 to 1000000 to test this. But when I try to create an array of this size, I get stack overflow exception.
    Below is the code that throws the exception

    Try this code, use new operator:

    unsigned int *a = new unsigned int[999999];

    Best Regards,

    Sera Yu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    • Proposed as answer by Baron Bi Friday, July 29, 2016 6:18 AM
    • Marked as answer by Hart Wang Friday, July 29, 2016 8:13 AM
    Wednesday, July 13, 2016 2:30 AM
  • For a large array, will the initial value of total_sum fit in an unsigned int?
    Wednesday, July 13, 2016 4:29 AM