2

I want to dynamically declare a boolean array for my C++ program and set its value to false.I am having issues with memory allocation I guess how and getting a dangling pointer I am trying to find prime numbers by Sieve of Eratosthenes I am trying to use boolean array dynamically but facing issues.

I am not trying to implement it using memset .

int  limit = 2000000 ;
int crossLimit = sqrt(limit) ;


void  SievePrime(int limit)
{

    bool* boolArray = new bool[false] ;

    for(int i =4;i<=limit ;i = i + 2)
    {
        boolArray[i] = true ; //getting error hereThread 1:                       
                         //EXC_BAD_ACCESS (code=2, address=0x100786000)
    }

    for(int j= 3 ;j<=crossLimit ; j= j+2){
        if (not boolArray[j])

        {
            for(int k =j*j;k<=limit;k*=2)
        {
            boolArray[k] = true ;
        }
    }
}

    double sum = 0 ;

    for(int i =2 ; i<=limit ;i++)
    {
        if(!boolArray[i])
        {
            sum = sum + i ;
        }
    }

    cout<<sum<<endl ;
}

expected output: 142913828922

error:Thread 1: EXC_BAD_ACCESS (code=2, address=0x100786000)

4
  • 3
    bool* boolArray = new bool[false] ; What do you think you're doing here ? Commented Jul 10, 2019 at 15:39
  • making the index values as false Commented Jul 10, 2019 at 15:43
  • 2
    The value that you gives in new bool[...] should be the size of your array, it doesn't correspond to the value of your array elements. So the problem is that you are iterating through an array that isn't well allocated Commented Jul 10, 2019 at 15:45
  • 1
    and here false is 0 Commented Jul 10, 2019 at 15:46

1 Answer 1

2

I sense that you're new to C++.

"false" is generally zero so you're not allocating anything.

bool* boolArray = new bool[false] ;

Try:

bool* boolArray = new bool[limit];


for(int i =0; i<limit; i+)
{
    boolArray[i] = false;                       
}

And put a delete at the end:

delete boolArray;

Also, that loop will overrun the bounds. If you have N items, the last valid entry is N-1 as the first is 0. So use k<limit in the for loop.

Sign up to request clarification or add additional context in comments.

3 Comments

ok that means array size has to be specified at declaration time.But what will be the value of items in those 2 million indexes .is it set to false by default?
@NajmusSaqibMakhdoomi Not unless you value-initialize them (and you can, via bool *boolArray = new bool[limit](); (note the parens at the end). Related, though the performance is less-than-stellar, std::vector<bool> ar(limit+1); would also work, free you from having to dynamically free at the end, and be much more inline with modern C++, where new and delete are almost never necessary in user programs. I added the +1 because your loops run through limit; not just one-short of it, and therefore you'll breach without the increased size.
This answer would be even better with an example of how an initialization loop would work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.