Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to correct an segmentation fault ?

how to correct an segmentation fault ?

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomgraphicsdata-structuresjson
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    below is an hackerrank question link where i am encountering an segmentation falut. HackerRank[^] and this is my solution

    #include
    #include
    using namespace std;

    void fillVector(vector &temp, unsigned long number);
    void RotateVector(vector &arr1, vector &arr2, unsigned long k);

    int main(){
    unsigned long n,k,q;

    cin >> n >> k >> q;
    
    vector a, b;
    fillVector(a, n);
    fillVector(b, q);
    
    RotateVector(a, b, k);
    
    
    return 0;
    

    }

    //to rotate the vector by kth measure.
    void RotateVector(vector &arr1, vector &arr2, unsigned long k){
    unsigned long arraySize = arr1.size();
    unsigned long querySize = arr2.size();
    unsigned long rotation = k, i;
    unsigned long start = rotation%arraySize; //this was causing the problem.

    if(rotation rotatedArray;
    
    for(i=0; i &arr, unsigned long n){
    unsigned long temp;
    for(int i=0; i\> temp;
        arr.push\_back(temp);
    }
    

    }

    the above solution is giving segmentation fault and i am not able to detect it.(rest all the test-cases are passed) here are the links to that test case inputs : https://hr-testcases-us-east-1.s3.amazonaws.com/1884/input04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1527377038&Signature=sA5C43bnnqyLeXwWA9L16en3rEE%3D&response-content-type=text%2Fplain and expected outputs : https://hr-testcases-us-east-1.s3.amazonaws.com/1884/output04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1527377042&Signature=%2BSyDUj0B5DwySkSPGjiwfmB7MlE%3D&response-content-type=text%2Fplain Edit : Below is the working code:

    #include
    using namespace std;

    void fillVector(vector

    L C 2 Replies Last reply
    0
    • T Tarun Jha

      below is an hackerrank question link where i am encountering an segmentation falut. HackerRank[^] and this is my solution

      #include
      #include
      using namespace std;

      void fillVector(vector &temp, unsigned long number);
      void RotateVector(vector &arr1, vector &arr2, unsigned long k);

      int main(){
      unsigned long n,k,q;

      cin >> n >> k >> q;
      
      vector a, b;
      fillVector(a, n);
      fillVector(b, q);
      
      RotateVector(a, b, k);
      
      
      return 0;
      

      }

      //to rotate the vector by kth measure.
      void RotateVector(vector &arr1, vector &arr2, unsigned long k){
      unsigned long arraySize = arr1.size();
      unsigned long querySize = arr2.size();
      unsigned long rotation = k, i;
      unsigned long start = rotation%arraySize; //this was causing the problem.

      if(rotation rotatedArray;
      
      for(i=0; i &arr, unsigned long n){
      unsigned long temp;
      for(int i=0; i\> temp;
          arr.push\_back(temp);
      }
      

      }

      the above solution is giving segmentation fault and i am not able to detect it.(rest all the test-cases are passed) here are the links to that test case inputs : https://hr-testcases-us-east-1.s3.amazonaws.com/1884/input04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1527377038&Signature=sA5C43bnnqyLeXwWA9L16en3rEE%3D&response-content-type=text%2Fplain and expected outputs : https://hr-testcases-us-east-1.s3.amazonaws.com/1884/output04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1527377042&Signature=%2BSyDUj0B5DwySkSPGjiwfmB7MlE%3D&response-content-type=text%2Fplain Edit : Below is the working code:

      #include
      using namespace std;

      void fillVector(vector

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You need to step through your code with the debugger to find out where the segv occurs.

      1 Reply Last reply
      0
      • T Tarun Jha

        below is an hackerrank question link where i am encountering an segmentation falut. HackerRank[^] and this is my solution

        #include
        #include
        using namespace std;

        void fillVector(vector &temp, unsigned long number);
        void RotateVector(vector &arr1, vector &arr2, unsigned long k);

        int main(){
        unsigned long n,k,q;

        cin >> n >> k >> q;
        
        vector a, b;
        fillVector(a, n);
        fillVector(b, q);
        
        RotateVector(a, b, k);
        
        
        return 0;
        

        }

        //to rotate the vector by kth measure.
        void RotateVector(vector &arr1, vector &arr2, unsigned long k){
        unsigned long arraySize = arr1.size();
        unsigned long querySize = arr2.size();
        unsigned long rotation = k, i;
        unsigned long start = rotation%arraySize; //this was causing the problem.

        if(rotation rotatedArray;
        
        for(i=0; i &arr, unsigned long n){
        unsigned long temp;
        for(int i=0; i\> temp;
            arr.push\_back(temp);
        }
        

        }

        the above solution is giving segmentation fault and i am not able to detect it.(rest all the test-cases are passed) here are the links to that test case inputs : https://hr-testcases-us-east-1.s3.amazonaws.com/1884/input04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1527377038&Signature=sA5C43bnnqyLeXwWA9L16en3rEE%3D&response-content-type=text%2Fplain and expected outputs : https://hr-testcases-us-east-1.s3.amazonaws.com/1884/output04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1527377042&Signature=%2BSyDUj0B5DwySkSPGjiwfmB7MlE%3D&response-content-type=text%2Fplain Edit : Below is the working code:

        #include
        using namespace std;

        void fillVector(vector

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        You correctly define start

        Quote:

        unsigned long start = rotation%arraySize;

        and then miss the opportunity of using it. By the way, in my opinion, you don't even need to actually rotate the vector.

        T 1 Reply Last reply
        0
        • C CPallini

          You correctly define start

          Quote:

          unsigned long start = rotation%arraySize;

          and then miss the opportunity of using it. By the way, in my opinion, you don't even need to actually rotate the vector.

          T Offline
          T Offline
          Tarun Jha
          wrote on last edited by
          #4

          thank you !

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups