-1

I am writing a c++ program in which I have filled a large array of size 100,000 with sequential values from 1 to 100,000. Now I am trying to make this array filled with prime numbers only. I have a variable defined "mult" which starts at 2 and goes through the array and deletes every 2nd location (multiples of 2), and I want loop again to do the same for 3, 5, 7, etc. However I get an exception thrown and my pointer p is a very large number. I would appreciate any clues as to why this is happening. Here is my code:

// Fill up the array with values 1 - 100,000.
void generateBigArray(int *p, int size)
{
int count = 1;
for (int i = 0; i < size; i++)
    {
        *p = count++;
        p = p + 1; // bump to the next location.
    }
}

void deleteMults(int *p, int size)
{
    int mult = 1;
    while (mult <= size)
    {
        for (int i = mult; i < size; i = i + mult)
        {
            p = p + 1; // starting at 2.
            mult = mult + 1;
            while (*p != 0)
            {
                *p = 0;
                p = p + mult;
            }
            mult = mult + 1;
        }
    }
 }


int main()
{
    // an array to hold prime numbers from 1 - 100,000
    const int BIGSIZE = 10000;
    int bigAry[BIGSIZE];

    // a pointer for the prime number array.
    int *bigptr;
    bigptr = bigAry;

    generateBigArray(bigptr, BIGSIZE);
    deleteMults(bigptr, BIGSIZE);

    return 0;
}
  • Don't allocate huge arrays using static `int x[N]` notation. Instead use `std::vector` or something like it where you can take advantage of both C++ containers and dynamic allocation. These are easily passed in by reference and don't need secondary "size" type parameters. – tadman Feb 26 '19 at 02:26
  • This is happening because you haven't yet learned how to use a debugger to run your program, one line at a time, examine the values of all variables, and see how they get initialized, and updated. Once you learn how to use your debugger, which is a required skill for every C++ developer, you should be able to easily debug your own code yourself, without having to ask anyone for help. Another possible reason is that your modern C++ compiler is capable of analyzing C++ source sufficiently enough to automatically detect this kind of a bug, but you are ignoring your compiler's warning. – Sam Varshavchik Feb 26 '19 at 02:27
  • 1
    Why do you have a counter `i` in your loop and then fail to use it? Consider `p[i] = i + 1` instead. You can also eliminate the `count` variable as it's redundant. – tadman Feb 26 '19 at 02:27
  • 1
    It looks like you're walking off the end of the array in the `deleteMults` function since you don't test that you're accessing something that's within the bounds defined in your `main` function. If `p` is advanced more than 10,000 places or more it will be invalid. Note, **10,000** and not 100,000 as you suggest in the comment. – tadman Feb 26 '19 at 02:29
  • Fun fact about prime numbers. You'll find that you're computing the same numbers over and over and over as you hunt for larger and larger primes. You're usually better off finding all of the primes up to the maximum number in one sweep and then looking in this list. A prime number sieve would speed this up greatly. – user4581301 Feb 26 '19 at 02:39
  • About terminology: the code does not **throw** an exception. That's a C++ thing, and the way to throw an exception is to write `throw something;`. What you're seeing is the OS complaining that the program screwed up. That's often reported as an "exception", but that's not the same word as "exception" in C++. – Pete Becker Feb 26 '19 at 15:00

1 Answers1

0

Depending on your compiler, the maximum size you can allocate for an array can vary greatly. Since you're reaffecting bigptr to bigAry, you could try something like:

int * bigptr = new int[BIGSIZE];

And before exiting the main, this should be added:

delete[] bigptr;
Kevin Pastor
  • 696
  • 3
  • 14
  • 1
    I recommend adding a note that the asker should prefer to use `std::vector` over `new`/`delete`. Here's a link to a good question should you feel the need to explain why: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Feb 26 '19 at 02:36