I've seen several questions related to this, but I want verification that I'm having a similar problem. My code allocates a boolean array with a very large number of elements. This is my code, compiling on an x86_64 Linux machine:
#include <iostream>
#include <math.h>
using std::cout;
using std::endl;
using std::nothrow;
long problem3()
{
long upper_bound = 600851475143;
long max_prime_factor = 1;
long max_possible_prime = (long) sqrt(upper_bound) + 1;
bool * primes;
primes = new (nothrow) bool[upper_bound];
primes[0] = false; //segmentation fault occurs here
primes[1] = false;
for (long i = 2; i < upper_bound; i++)
primes[i] = true;
for (long number = 2; number < max_possible_prime; number++)
{
if (primes[number] == true)
{
if (upper_bound % number == 0)
{
max_prime_factor = number;
}
for (long j = number + number; j < upper_bound; j += number)
primes[j] = false;
}
else { continue; }
}
return max_prime_factor;
}
int main ( int argc, char *argv[] )
{
cout<<"Problem 3: "<<problem3()<<endl;
}
Building this code and running it as is gives a segmentation fault on this line:
primes[0] = false
If I remove the nothrow
instruction to change this line:
primes = new (nothrow) bool[upper_bound];
to this:
primes = new bool[upper_bound];
I get an error message stating:
terminate called after throwing an instance of 'std::bad_alloc'
I think this means that the allocation is failing, presumably because of the size (based on similar questions and other referenced links.
The debugger in CodeBlocks shows that primes
remains set to 0x0
even after its supposed to be allocated. Valgrind confirms this:
==15436== Command: ./main
==15436==
==15436== Invalid write of size 1
==15436== at 0x400A81: problem3() (main.cpp:54)
==15436== by 0x400B59: main (main.cpp:77)
==15436== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==15436==
==15436==
==15436== Process terminating with default action of signal 11 (SIGSEGV)
==15436== Access not within mapped region at address 0x0
==15436== at 0x400A81: problem3() (main.cpp:54)
==15436== by 0x400B59: main (main.cpp:77)
==15436== If you believe this happened as a result of a stack
==15436== overflow in your program's main thread (unlikely but
==15436== possible), you can try to increase the size of the
==15436== main thread stack using the --main-stacksize= flag.
==15436== The main thread stack size used in this run was 8388608.
==15436==
==15436== HEAP SUMMARY:
==15436== in use at exit: 0 bytes in 0 blocks
==15436== total heap usage: 1 allocs, 0 frees, 0 bytes allocated
==15436==
==15436== All heap blocks were freed -- no leaks are possible
==15436==
==15436== For counts of detected and suppressed errors, rerun with: -v
==15436== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 3 from 3)
Segmentation fault
Question: I know about std::vector
, so should I use that to allocate this array? I'm open to trying a different algorithm as well, but I would like to know if there is a nuance of C++ I'm missing that will allow me to allocate such an array (even though it's absolutely massive and I understand that). I tried to debug this question as much as possible too, but if there is anything else I should have provided, let me know so I can use the tools the next time I run into trouble.