Search code examples
c++compiler-errorsruntime-error

Why is this code giving me segmentation, ? when we print only count then it gives answer and when print ans[4] or any value then it gives error?


I am getting segmentation error , it gives output when only print ans[k] inside the function then it 
   gives corrct output but in main when I try to print ans[k] then it gives segmentation 
i am new at progamming so i don't know more about it so please help me it's my assigment 

question is

I have to create a array of factors of a number upto 4 * 10^6

Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.”

When a piece of code tries to do read and write operation in a read only location in memory or freed block of memory, it is known as core dump. It is an error indicating memory corruption.

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long int
    vector<int> arr(1000001,0);
    vector<int> ans;
    
    bool isPrime(int n)
    {
    
        if (n <= 1)
            return false;
        if (n <= 3)
            return true;
    
        if (n % 2 == 0 || n % 3 == 0)
            return false;
    
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
    
        return true;
    }
    
     
    
    void factorpush()
    {
    
        for (int k = 5; k <= 4000001; k += 4)
        {
            if (isPrime(k))
            {
                arr[k] = 1;
            }
        }
        
        arr.clear();
        ans.clear();
        for (int k = 5; k <= 4000001; k += 4)
        {
            if (arr[k] == 1)
            {
                int n = (k / 4);
    
                ans[n] = (2 * n) - 1 + k;
                 
            }
    
            else
            {
                vector<int> v;
    
                for (int i = 1; i*i < k; i++)
                {
                    if (k % i == 0)
                    {
    
                        if (k / i == i && i != k)
                            v.push_back(i);
    
                        else
                      {
                        if (i != k)
                        {
                            v.push_back(i);
                        }
    
                        if (k / i != k)
                        {
                            v.push_back(k/ i);
                        }
                      }
                    }
                }
    
                int count = k;
                int count2 = 0;
                for (auto x : v)
                {
                    if (x != 1 && x!=k)
                    {
                        int n = (k/4);
                        count2 += ((2*n - 1)/x);
                        count +=  ((2*n - 1)/x)*x;
                    }
                    
                }
                
                 int n1 = (k/4);
                  int val = (2*n1) - 1 - count2;
                    
                  count += val;
                  
                   ans[n1] = count;
                 //cout<<ans[n1]<<"\n"; 
                  
            }
        }
    }
    
    int32_t main()
    {
        factorpush();
        int n;
        cin>>n;
        
        cout<<ans[n]<<"\n";` 
                  return 0;
    }

Solution

  • I didn't read your code well and there may be other errors, but at least the combination of

        vector<int> arr(1000001,0);
    

    and

            for (int k = 5; k <= 4000001; k += 4)
            {
                if (isPrime(k))
                {
                    arr[k] = 1;
                }
            }
    

    is bad because arr[k] will go further than allocated.

    You have to allocate enough elements like

        vector<int> arr(4000002,0);
    

    Also accessing arr[k] and ans[n] after arr.clear(); and ans.clear(); and without adding any elements is bad because the clear() function erases all elements from the vector. Also checking for arr[k] == 1 after the erasure doesn't make sense. Not understanding your code well, it looks like

            arr.clear();
            ans.clear();
    

    should be replaced with something like

            ans.clear();
            ans.resize(1000001);