Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

c++ - Segmentation fault with vectors, but not sure what I did wrong

So when I run this code:

#include <iostream>
using namespace std;
#include <vector>
#include <string>
int main()
{
   int current_number = 0;
   vector<int>primes;
   for(int i = 0; i < 100; i++)
   {
      current_number++;
      for(int h= 0; h < primes.size(); h++)
      {
         if(current_number % primes[h] == 0)
         {
            continue;
         }
         else
         {
            primes.push_back(current_number);
         }
      }
   }
   for(int x =0; x < 100; x++)
   {
      cout << primes[x];
   }
}

I get a segmentation fault. Im pretty sure it has something to do with vectorprimes. But, I'm not sure exactly what. The purpose of the code is to find every prime number between 1 and 100.

question from:https://stackoverflow.com/questions/65875684/segmentation-fault-with-vectors-but-not-sure-what-i-did-wrong

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Among the problems in the posted code.

  • The inner loop body will never be entered because primes is initially empty the only code that changes it is in that loop.
  • Even after fixing the initial primes content with {2} and starting the counter loop with 3, the logic in the inner loop is still wrong. It appends on ever non-zero modulo. That shouldn't be done at all in the inner loop. Rather, the loop should break on any zero-modulo, and the outer loop then only appends to primes when it knows the inner loop didn't break early.
  • The final reporting loop assumes there are 100 primes in the first 100 numbers, which clearly isn't the case. Either it should be iterating based on container size, using iterators, or better still, just used ranged-for.
  • Minor: the current_number is pointless; just use i from the outer loop and start it at 3.
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> primes = {2};
    for (int i = 3; i <= 100; i++)
    {
        bool isprime = true;
        for (auto x : primes)
        {
            if (i % x == 0)
            {
                isprime = false;
                break;
            }
        }

        if (isprime)
            primes.emplace_back(i);
    }

    for (auto x : primes)
        std::cout << x << ' ';
    std::cout << '
';
}

Output

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Note

There are better ways to do this. I suggest you investigate how to build a Sieve of Eratosthenes or similar sieve. In truth, the above code is already over halfway there. It wouldn't take much more to do it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...