Run-time Check Failure #2 - Stack around the variable "primes" was corrupted

Avionix

I have looked at almost all the other Run-time Check Failure #2 problems, and only 1 has applied the error to the same location I have as in my program. The error, as commented, occurs after I end main(). I am not allocating past the end of the array, and am not changing anything after main has returned.

#include <iostream>

void findPrimes(bool primes[], const int arrSize);
int main(){
    const int arrSize = 1000;
    bool primes[arrSize];
    for (int x = 0; x < arrSize; x++){
        primes[x] = true;
    }
    findPrimes(primes, arrSize); //sets all non-prime numbers to false
    for (int x = 0; x < arrSize; x++){ //I did not go past the size of the array.
        if (primes[x]){
            std::cout << x << std::endl;
        }
    }
    return 0; //Error occurs after this point.
}

void findPrimes(bool primes[], const int arrSize){ //detects and changes non-prime numbers to false
    int temp;
    for (int x = 2; x < arrSize; x++){
        temp = x + x;
        for (int c = 0; c < arrSize; c++){
            if (temp > arrSize){
                break;
            }
            primes[temp] = false;
            temp += x;
        }
    }
}
templatetypedef

Your test

if (temp > arrSize){
        break;
}

is on the right track to make sure you don't overrun the array bounds, but there's an off-by-one error because the index arrSize isn't a valid array index but won't cause the loop to break at this point. Change this to read

if (temp >= arrSize){
        break;
}

and see if that fixes things.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Run-Time Check Failure #2 - Stack around the variable '' was corrupted

From Dev

Run-Time Check Failure #2 - Stack around the variable '...' was corrupted

From Dev

Why am I getting Run-Time Check Failure #2 - Stack around the variable 'x' was corrupted?

From Java

Run-Time Check Failure #2 - Stack around the variable 'sortObject' was corrupted. how to fix?

From Dev

Run-Time Check Failure #2 - Stack around the variable 'foo' was corrupted

From Dev

C++ - Run-Time Check Failure #2 - Stack around variable 'sourceCount' was corrupted

From Dev

Run-Time Check Failure #2 - Stack around the variable 'd' was corrupted

From Dev

Basic C++ error. Run-Time Check Failure #2 - Stack around the variable 'matrix' was corrupted

From Dev

C++ - Run-Time Check Failure #2 - Stack around variable 'sourceCount' was corrupted

From Dev

Why am I getting Run-Time Check Failure #2 - Stack around the variable 'x' was corrupted?

From Dev

Run-Time Check Failure #2 - Stack around the variable 'char' was corrupted

From Dev

Run-Time Check Failure #2 - Stack around the variable 'result' was corrupted

From Dev

Run-Time Check Failure #2 - Stack around the variable 'obj' was corrupted

From Dev

Run-Time Check Failure #2 - Stack around the variable 'numberchoices' was corrupted

From Dev

RunTime Check Failure #2 - Stack around the variable "tab" was corrupted

From Dev

Stack around the variable was corrupted

From Dev

Stack around variable was corrupted

From Dev

Stack around variable was corrupted

From Dev

stack around the variable...was corrupted

From Dev

"Stack around the variable was corrupted" error

From Dev

Stack around variable 'x' was corrupted

From Dev

Stack around the variable 'sortArray' was corrupted

From Dev

"Stack around the variable was corrupted" error

From Dev

Stack around variable was corrupted - C

From Dev

C - Stack around the variable 'name' was corrupted

From Dev

C: Error with stack around the variable 's' was corrupted

From Dev

Getting error: Stack around the variable was corrupted

From Dev

Stack around the variable was corrupted with pointer arithmetics

From Dev

Array issues: Stack around the variable 'arr' was corrupted

Related Related

  1. 1

    Run-Time Check Failure #2 - Stack around the variable '' was corrupted

  2. 2

    Run-Time Check Failure #2 - Stack around the variable '...' was corrupted

  3. 3

    Why am I getting Run-Time Check Failure #2 - Stack around the variable 'x' was corrupted?

  4. 4

    Run-Time Check Failure #2 - Stack around the variable 'sortObject' was corrupted. how to fix?

  5. 5

    Run-Time Check Failure #2 - Stack around the variable 'foo' was corrupted

  6. 6

    C++ - Run-Time Check Failure #2 - Stack around variable 'sourceCount' was corrupted

  7. 7

    Run-Time Check Failure #2 - Stack around the variable 'd' was corrupted

  8. 8

    Basic C++ error. Run-Time Check Failure #2 - Stack around the variable 'matrix' was corrupted

  9. 9

    C++ - Run-Time Check Failure #2 - Stack around variable 'sourceCount' was corrupted

  10. 10

    Why am I getting Run-Time Check Failure #2 - Stack around the variable 'x' was corrupted?

  11. 11

    Run-Time Check Failure #2 - Stack around the variable 'char' was corrupted

  12. 12

    Run-Time Check Failure #2 - Stack around the variable 'result' was corrupted

  13. 13

    Run-Time Check Failure #2 - Stack around the variable 'obj' was corrupted

  14. 14

    Run-Time Check Failure #2 - Stack around the variable 'numberchoices' was corrupted

  15. 15

    RunTime Check Failure #2 - Stack around the variable "tab" was corrupted

  16. 16

    Stack around the variable was corrupted

  17. 17

    Stack around variable was corrupted

  18. 18

    Stack around variable was corrupted

  19. 19

    stack around the variable...was corrupted

  20. 20

    "Stack around the variable was corrupted" error

  21. 21

    Stack around variable 'x' was corrupted

  22. 22

    Stack around the variable 'sortArray' was corrupted

  23. 23

    "Stack around the variable was corrupted" error

  24. 24

    Stack around variable was corrupted - C

  25. 25

    C - Stack around the variable 'name' was corrupted

  26. 26

    C: Error with stack around the variable 's' was corrupted

  27. 27

    Getting error: Stack around the variable was corrupted

  28. 28

    Stack around the variable was corrupted with pointer arithmetics

  29. 29

    Array issues: Stack around the variable 'arr' was corrupted

HotTag

Archive