Can someone tell me why this compiles and runs fine in visual studio, but fails with a segmentation fault when compiled GNU C++ compiler

can't for the life of me figure this out. I went through and checked the prefix/postfix operators and I made sure that the iterators were placed properly in conditional statements... what am I missing? usually I get this problem when I am working with allocating/deallocating dynamic arrays, which I am not doing here.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <random>
#include <unordered_set>

namespace std{
    template <>
   struct hash<vector<int>> : public unary_function<vector<int>, size_t>
   {
       size_t operator()(const vector<int>& v) const
       {
           return v.size();
       }
   };
}

using namespace std;

struct testCase{
    int n; 
    int m;
    vector <int> wordVector; 
};

bool lexOrderConfirm(std::vector<int>::const_iterator, std::vector<int>::const_iterator, std::vector<int>::const_iterator, int ); 

int main() {    

int t; 

cin>>t;
vector <testCase*> testCaseVector(t);

for (int i = 0; i < t; ++i){

     testCaseVector[i] =  new testCase;

     cin>>testCaseVector[i]->n; 
        cin>>testCaseVector[i]->m;

}




         for (int i = 0; i < t; ++i){


            std::unordered_set<std::vector<int>, std::hash<std::vector<int> > >permutations;

             int permu = pow((double)testCaseVector[i]->n, testCaseVector[i]->m); 





int count = 0;



      for (int z= 0;  z < (permu*10); ++z){
      std::random_device rd;

    std::default_random_engine randomeng( rd() );
     int v;
      for (int b = 0; b < testCaseVector[i]->m; ++b){
     v= randomeng() % testCaseVector[i]->n + 1;   /*random number between 1 and n*/
     testCaseVector[i]->wordVector.push_back(v);
      }
      std::sort(testCaseVector[i]->wordVector.begin(), testCaseVector[i]->wordVector.end());

      permutations.insert(testCaseVector[i]->wordVector);
      testCaseVector[i]->wordVector.clear();
      }



      do {

           vector <int> test = *permutations.begin();

        do {
                std::vector<int>::const_iterator start = test.begin();
            std::vector<int>::const_iterator end;

                end = test.end();

            std::vector<int>::const_iterator lastElem = start+(testCaseVector[i]->m-1);


            if (lexOrderConfirm(start, end, lastElem, testCaseVector[i]->n)){
                ++count;
            }


        }while(std::next_permutation(test.begin(),test.end()));

        permutations.erase(permutations.begin());
        test.clear();
      }while(!permutations.empty());







  count = count%100000007;
        cout<<count<<endl;



         }
    return 0;

}


bool lexOrderConfirm(std::vector<int>::const_iterator start, std::vector<int>::const_iterator end, std::vector<int>::const_iterator lastElem, int N){
  bool confirmed = true;

  for (std::vector<int>::const_iterator t = start; t != end; ++t){
                if ((2* (*t) > N)){


               } else if ((2*(*t) <= N) && (*(t+1) >= 2 * (*t)) && (t != (lastElem))){



                }else{
                    confirmed = false;
                    break;
               }

    }

  return confirmed;
}
有帮助吗?

解决方案

The reason is, of course, you are dereferencing some uninitialized pointer. It is impossible to tell from your current code because for one thing it is dependent on user input. It seems to run just fine for me with t = 2; Even though I am not sure if it is working as it is supposed to.

However why does it run on Visual Studio?

Because VC++ doesn't initialize pointers to NULL. It is working becasue what ever is pointed by the uninitialized pointer is somehow working as the initialized one would. So basically Undefined behavior.

Why does it fail with a segmentation fault when compiled GNU C++ compiler?

Because g++ does initialize pointers to NULL

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top