简体   繁体   中英

g++ Internal compiler error in small program

I am writing a small program to solve Project Euler Problem 21 , and I was testing out the early parts of my code when I ran into an unexplained internal compiler error. I'd appreciate any tips on how to re-write my program to avoid this kind of error.

Here's my build command and compiler options:

g++ -std=c++11 -O2 -Wall -o "pe_021" "pe_021.cc"

Here's the error that I get:

Internal compiler error: Error reporting routines re-entered.  
Compilation failed.

Here's my code:

#include <array>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

const unsigned int N = 10000; //look for amicable numbers smaller than N
vector<unsigned int> list_of_primes;

vector<unsigned int[2]> prime_factorize(unsigned int);

int main(int argc, char **argv)
{
    //import a list of primes
    ifstream ifs ("primes_10000.txt");
    for (unsigned int index = 0; index < N; index++) {
        string prime_number;
        getline(ifs, prime_number);
        list_of_primes.push_back(stoi(prime_number));
    }
    ifs.close();

    //test prime factorization function by prime factorizing 12
    vector<unsigned int[2]> prime_factorization = prime_factorize(12);
    for(unsigned int (&prime_and_exponent)[2] : prime_factorization) {
        cout << prime_and_exponent[0] << ", " << prime_and_exponent[1] << endl;
    }

    return 0;
}

vector<unsigned int[2]> prime_factorize(unsigned int number)
{
    vector<unsigned int[2]> prime_factorization;
    for(unsigned int index = 1; index < list_of_primes.size(); index++) {
        if(number % list_of_primes[index] == 0) {
            unsigned int prime_and_exponent[2] = {list_of_primes[index], 1};
            prime_factorization.push_back(prime_and_exponent);
            number /= list_of_primes[index];

            while(number % list_of_primes[index] == 0) {
                prime_factorization.back()[1]++;
                number /= list_of_primes[index];
            }
        }
        if(number == 1) {
            break;
        }
    }
    return prime_factorization;
}

The error is caused by line

  prime_factorization.push_back(prime_and_exponent);

It seems like putting arrays into vectors leads to crash at least in GCC 4.7.3. First of all, send bug report to GCC developers. Second, wrap your unsigned int[2] into class, I think this should work.

UPD: as pointed out in comments, putting an array into a vector is illegal.

Replaced occuerences of unsigned int[2] with pair<unsigned int,int unsigned> . You will be much happier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM