Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack overflow (parameters: 0x00000001, 0x00442FF8)

Tags:

c++

Algorithm checks if both variables "a" and "x" are prime numbers.If yes, it simply announce that these are prime.Need 50 of them, when it comes to 6th position program shows an error:

Exception thrown at 0x00D02509 in ConsoleApplication3.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x002D2F5C). Unhandled exception at 0x00D02509 in ConsoleApplication3.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x002D2F5C).

#include "stdafx.h"
#include <iostream>

using namespace std;


int CheckIfPrime(long int n)
{
    if (n<2) return 0;
    for (int i = 2; i*i <= n; i++)
        if (n%i == 0) return 0;
    return 1;
}

int pow(int ap, int nt)
{
    if (nt == 0)
        return 1;
    else
        return ap *= pow(ap, --nt);
}

void CountA(int *aValue, int x)
{
    *aValue = (pow(2, x) - 1);
}

int main()
{
    int x = 1;
    int a = 0;
    int *aPointer = &a;
    for (int i = 0; i <= 50;)
    {
        x++;
        if (CheckIfPrime(x))
        {
            CountA(aPointer, x);
            if (CheckIfPrime(a))
            {
                cout << i << ". X = " << x << " a = " << a  << " are prime " << endl;
                    i++;
            }
        }
        else
        {
            cout << "";
        }
    }
    getchar();
    return 0;
}
like image 632
Johny Lavers Avatar asked Mar 09 '26 00:03

Johny Lavers


1 Answers

You have stack overflow in function:

int pow(int ap, int nt)
{
  if (nt == 0)
    return 1;
  else
    return ap *= pow(ap, --nt);
}

for very large nt it will enter too deeply in recursion causing SO

like image 82
marcinj Avatar answered Mar 10 '26 13:03

marcinj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!