0
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;
    }
Evg
  • 25,259
  • 5
  • 41
  • 83
  • 3
    Warning: `#include ` [loads the gun](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). `using namespace std;` [takes off the safety](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Be very, very careful. As an aside, the time you save typing `#include ` instead of the required includes is quickly eaten up by the build process taking about ten times as long. This time-saving maneuver often costs you time after two or three compiles. – user4581301 May 14 '21 at 17:23

1 Answers1

0

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);
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Similarly, as far as I can tell the code doesn't properly put values into `ans` at all, it just immediately starts writing to `ans[n]` – Nathan Pierson May 14 '21 at 16:09
  • @NathanPierson Yes, `factorpush()` is called before printing `ans[n]`, but the function in the original code doesn't add any elements to `ans`. – MikeCAT May 14 '21 at 16:12