Segmentation Fault :(

This is a discussion on Segmentation Fault :( within the C++ Programming forums, part of the General Programming Boards category; who's theorum? lol thanks for pointing this out, though. yes, sorry, those &&s change to ||s.. fingers' fault....

  1. #31
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    who's theorum? lol thanks for pointing this out, though.

    yes, sorry, those &&s change to ||s.. fingers' fault.
    Last edited by nadroj; 04-06-2007 at 01:17 AM.

  2. #32
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    i mean how would/ should i redo my code becuase i'm getting fed up with this already. i mean i think you see what i want to do, i just can't figure out how to do it exactly

  3. #33
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i cant see what you want to do, but i havent really looked at the big while loop.. ill look in the morning.

  4. #34
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    damm thanks

  5. #35
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    ok i completely re did my code and i get a fing segmentation fault. Where is it, i think this code is much better than the previous one i wrote. I ran GDB this is what it said for thing code
    Code:
    26          queue<rider> backIn;
    (gdb) 
    30          string  inFileName = "riders.dat";
    (gdb) 
    34          string  outFileName = "riders.out";
    (gdb) 
    37          ifstream inFile;
    (gdb) 
    40          ofstream outFile;
    (gdb) 
    43          inFile.open(inFileName.c_str());
    (gdb) 
    46          outFile.open(outFileName.c_str());
    (gdb) 
    49          rider customer;
    (gdb) 
    51          rider transfer = arrivals.front(); 
    (gdb) 
    
    Program received signal SIGSEGV, Segmentation fault.
    0xb7f2e1bf in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string () from /usr/lib/libstdc++.so.6
    any idea what this means?

    Code:
     
    
        //creates customer of the struct rider;
        rider customer;
    
        rider transfer = arrivals.front();
    
        rider onRide = line.front(); 
    
        while(inFile.good())
        {
            //it will loop and read from the inFile data while the customer data
            //is not equal to END 0 0 
            while(customer.lastName != "END"))
            {
                //here the file will place the correct data for each customer into 
                //the correct variables
                inFile >> customer.lastName >> customer.tickets >> customer.time;
    	
                //after the customer in that particular read is done putting
                //it in the correct variables the customer gets pushed 
                //into arrivals queue
                arrivals.push(customer);
            }
            
            transfer.time = clock;
            while(transfer.time == clock)
            {
                line.push(transfer);
                arrivals.pop();
            }
       
            while(!(arrivals.empty() && line.empty()))
            {
                onRide.tickets--;
                if(onRide.tickets == 0)
                {
                    outFile << onRide.lastName << clock+1 << endl;
                }
                else
                {
                    backIn.push(onRide);
                }
                clock++;
                if(transfer.time > clock)
                {
                    clock = transfer.time;
                }
                while(transfer.time == clock)
                {
                    line.push(transfer);
                    arrivals.pop();
                }
                if(!backIn.empty())
                {
                    line.push(backIn.front());
                    backIn.pop();
                }
            }
         }
    
        //closes the infile
        inFile.close();
    
        //closes the outfile          
        outFile.close();
    
        //signals ok to the os
        return 0;
    };
    Last edited by DarkDot; 04-06-2007 at 02:10 AM.

  6. #36
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    From what I read and understand of the instructions for this that were posted in this thread Queue Project Problem you would use the clock variable to determine when you will add the item to the end of the line. It seems to me that you are setting the clock equal to the item in the queue when you should be comparing the entrance time to the clock to see if it should be added to the queue.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  7. #37
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It looks, like you are still using the front() rider from queues that are empty.

    What would arrivals.front() return before you put anything in it?

    The way you do "time-travel" in your code also seems suspicious. There are situations where backIn is not empty (it's a temporary container, why can't you push onRide directly into the line, if he has tickets left?), but the clock gets turned forward according to the next arriver.

    So if the first person arrived at time 1 and second at time 10, the first person can take only one ride and then the clock gets suddenly turned forward and he has to stand in queue with the second arriver?

    (By the way, congratulations on removing the comments. If your teacher insists on that, add them as the last thing, because they really hindered reading the code.)

    The code required is not very complicated, so may-be you could rewrite it from scratch (with a little better plan), rather than modifying it to every suggestion. A simple way to determine if the main loop should continue, is to keep a counter of people visiting the amusement park. First set it equal to the number of people planning to visit (arrivals), each time someone runs out of tickets decrement it. When it reaches 0, all riders will be served.
    Last edited by anon; 04-06-2007 at 05:24 AM.

  8. #38
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    i have new updated code and i am still getting a segmentation fault i ran thriugh gdb and tried to fix but to no avail. If you think/know whats wrong please let me know where in the code specifically it is.
    Code:
    #include <queue>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "rider.h"
    
    using namespace std;
    
    int main()
    {
        //variable used to keep time
        int clock;
    
        //queue that the information from the infile gets stored into 
        queue<rider> arrivals;
    
        //queue that the riders with the correct clock time get put into
        queue<rider> line;
    
        //queue that the rider that needs to be put back in line gets held
        queue<rider> backIn;
    
        //string that makes it easier to remember the name of the file to open
        //to read from
        string  inFileName = "riders.dat";
    
        //string that makes it easier to remember the name of the file to open
        //to write to
        string  outFileName = "riders.out";
       
        //file read from
        ifstream inFile;
    
        //file write to
        ofstream outFile;
        
    inFile.open(inFileName.c_str());
        if (!inFile)
        {
            // error if not inFilename entered when prompted
            cout << "ERROR: Unable to open the inFile";
            exit(1);
        }
    
    	//open outFile
    	outFile.open(outFileName.c_str());
        if (!outFile)
        {
            // error if not outFilename entered when prompted
            cout << "ERROR: Unable to open the outFile";
            exit(1);
        }
        //creates customer of the struct rider;
        rider customer;
        
        //here the file will place the correct data for each customer into 
        //the correct variables
        inFile >> customer.lastName >>  customer.time >> customer.tickets;
             
    
            //it will loop and read from the inFile data while the customer data
            //is not equal to END 0 0 
            while(!(customer.lastName == "END" && customer.tickets == 0 && customer.time == 0))
            { 
    	
                //after the customer in that particular read is done putting it in the
                //correct variables the customer gets pushed ito arrivals queue
                arrivals.push(customer);
    
                //here the file will place the correct data for each customer into 
                //the correct variables
                inFile >> customer.lastName >>  customer.time >> customer.tickets;
             
            }
    
        inFile.close();
    
        //once all the customers are read from the inFile and put into the arrivals
        //queue the program and clock will have to be run where this data will be 
        //processed. For the processing part of the program, it will run while
        //the queues arrivals and line are not empty, therefore they will stop
        //running when they become empty.
        while(!(arrivals.empty() && line.empty() && backIn.empty()))
        { 
    
                rider transfer = arrivals.front();
    
                if(transfer.time > clock)
                {
                //sets clock equal to the first customers time
                clock = transfer.time;            
                }       
      
                //will put all the customers with the same arrival 
                //time as the clock into the line queue
                while(transfer.time == clock)
                {
                //here the front of the arrivals queue is put into the line queue
                line.push(transfer);
    
                //here the old front from the arrivals queue is popped off
                arrivals.pop();
    
                transfer = arrivals.front();
                }
    
                //need to check to see if backIn is empty or not, if it is
                //then it won't add anything, if not it will add the customers data 
                if(!backIn.empty())
                {
                
                //if the rider from the previous time still has tickets left it
                //will be added after the next group of customers from that time
                line.push(backIn.front());
               
                //get ride of the rider in the backIn queue
                backIn.pop();
                }  
    
                //after the customers are added to the line queue the ride 
                //will begin to run, which entails taking away one ticket 
                //from the person on the ride then checking to see if any 
                //tickets are left and if so add to the end of the line queue
                //after the next group of customers enter, if no tickets are 
                //left then write out the name and the clock time to the out file.
    
                //creates a variable onRide to represent the front of the line
                //queue which represents that customer going on the ride
                rider onRide = line.front();
    
                //takes 1 ticket way from the customer that is going on the line
                onRide.tickets--;
    
                //increments clock 
                clock++;
    
                //after the ride is completed it checks to see if the customer
                //that was just on the ride has more tickets to go back in line
                //or if it should be written to the outfile
    
                //if the customer on ride has 0 tickets the last name and the
                //clock time get printed out on the line to the outfile
                if(onRide.tickets == 0)
                {
                    //write the customers lastName and clock time to the outfile
                    outFile << onRide.lastName << clock << endl;
    
                    line.pop();
              
                    onRide = line.front();
                }
                //else if the cusomters ticket count is greater than 0 the customer
                //and his info will get stored into backIn to place the customer 
                //back in line
                else
                {
                    //places the customer on the ride's data into the backIn 
                    //variable so they may get placed back in line 
                    backIn.push(onRide);
                    
                    line.pop();
                     
                    onRide = line.front();
                }
    /*         
        //this will look to see if the transfer arrival time is equal to 0, if not
        //it will set clock equal to that number then copy all the transfers that
        //have that clock number.
        
            //checks to see if the transfer time is equal to the clock time
            if(!line.empty())
            {
    
                //will put all the customers with the same arrival 
                //time as the clock into the line queue
                while(transfer.time == clock)
                {
                //here the front of the arrivals queue is put into the line queue
                line.push(transfer);
    
                //here the old front from the arrivals queue is popped off
                arrivals.pop();
    
                transfer = arrivals.front();
                }
               
                //need to check to see if backIn is empty or not, if it is
                //then it won't add anything, if not it will add the customers data 
                if(!backIn.empty())
                {
                
                //if the rider from the previous time still has tickets left it
                //will be added after the next group of customers from that time
                line.push(backIn.front());
                    
                //remove the rider in backIn
                backIn.pop();
    
                } 
    
                //increment clock
                clock++;
    
                //after the customers are added to the line queue the ride 
                //will begin to run, which entails taking away one ticket 
                //from the person on the ride then checking to see if any 
                //tickets are left and if so add to the end of the line queue
                //after the next group of customers enter, if no tickets are 
                //left then write out the name and the clock time to the out file.
            
                //creates a variable onRide to represent the front of the line
                //queue which represents that customer going on the ride
                onRide = line.front();
    
                //takes 1 ticket way from the customer that is going on the line
                onRide.tickets--;
    
                //after the ride is completed it checks to see if the customer
                //that was just on the ride has more tickets to go back in line
                //or if it should be written to the outfile
    
                //if the customer on ride has 0 tickets the last name and the
                //clock time get printed out on the line to the outfile
                if(onRide.tickets == 0)
                {
                    //write the customers lastName and clock time to the outfile
                    outFile << onRide.lastName << clock << endl;
    
                    line.pop();
    
                    onRide = line.front();
    
                }
                //else if the cusomters ticket count is greater than 0 the customer
                //and his info will get stored into backIn to place the customer 
                //back in line
                else
                {
                    //places the customer on the ride's data into the backIn 
                    //queue so they may get placed back in line 
                    backIn.push(onRide);
                    
                    line.pop();
    
                    onRide = line.front();
                
                }
            }*/ 
            }
        //closes the outfile          
        outFile.close();
    
        //signals ok to the os
        return 0;
    };

  9. #39
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    if(transfer.time > clock)
    give clock a value when you declare it near the top of the code. im sure this will affect your program, but i doubt its the segfault--still looking.

    edit: you have a giant block-comment--are we supposed to ignore its content?
    Last edited by nadroj; 04-06-2007 at 11:16 PM.

  10. #40
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Here, I added some asserts for you. See where the queue whose front element you are trying to read is empty. (This doesn't mean there's nothing else wrong with the code.)

    If you fix those problems, you still have the infinite loop that has already been pointed out. pop() just removes the first element from the queue, it doesn't magically change transfer.

    All in all, the logic is messy. A complete rewrite (with a clearer logic) could get you there.

    And when posting code again, please consider the comments below.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <queue>
    #include <cassert>
    
    using namespace std;
    
    struct rider
    {
        //variable for last name of rider
        string lastName;
    
        //variable for number of tickets the rider has
        int tickets;
    
        //variable for the arrival time of the rider
        int time;
    };
    
    int main()
    {
        //variable used to keep time
        int clock;
    
        //queue that the information from the infile gets stored into 
        queue<rider> arrivals;
    
        //queue that the riders with the correct clock time get put into
        queue<rider> line;
    
        //queue that the rider that needs to be put back in line gets held
        queue<rider> backIn;
    
        //string that makes it easier to remember the name of the file to open
        //to read from
        string  inFileName = "riders.dat";
    
        //string that makes it easier to remember the name of the file to open
        //to write to
        string  outFileName = "riders.txt";
       
        //file read from
        ifstream inFile;
    
        //file write to
        ofstream outFile;
        
        inFile.open(inFileName.c_str());
        if (!inFile)
        {
            // error if not inFilename entered when prompted
            cout << "ERROR: Unable to open the inFile";
            exit(1);
        }
    
    	//open outFile
    	outFile.open(outFileName.c_str());
        if (!outFile)
        {
            // error if not outFilename entered when prompted
            cout << "ERROR: Unable to open the outFile";
            exit(1);
        }
        //creates customer of the struct rider;
        rider customer;
        
        //here the file will place the correct data for each customer into 
        //the correct variables
        inFile >> customer.lastName >>  customer.time >> customer.tickets;
             
    
            //it will loop and read from the inFile data while the customer data
            //is not equal to END 0 0 
            while(!(customer.lastName == "END" && customer.tickets == 0 && customer.time == 0))
            { 
    	
                //after the customer in that particular read is done putting it in the
                //correct variables the customer gets pushed ito arrivals queue
                arrivals.push(customer);
    
                //here the file will place the correct data for each customer into 
                //the correct variables
                inFile >> customer.lastName >>  customer.time >> customer.tickets;
             
            }
    
        inFile.close();
    
        assert(clock == 0 && "clock probably not initialized.");
        
        while(!(arrivals.empty() && line.empty() && backIn.empty()))
        { 
                assert(!arrivals.empty() && "arrivals must not be empty!");
                rider transfer = arrivals.front();
    
                if(transfer.time > clock)
                {
                //sets clock equal to the first customers time
                clock = transfer.time;            
                }       
      
                while(transfer.time == clock)
                {
                //here the front of the arrivals queue is put into the line queue
                line.push(transfer);
                assert(line.size() <= 4 && "How can you have more than 4 people in the line?");
    
                //here the old front from the arrivals queue is popped off
                assert(!arrivals.empty() && "arrivals must not be empty!");
                arrivals.pop();
                
                assert(!arrivals.empty() && "arrivals must not be empty!");
                transfer = arrivals.front();
                }
                if(!backIn.empty())
                {
                assert(!backIn.empty() && "backIn must not be empty!");
                line.push(backIn.front());
               
                //get ride of the rider in the backIn queue
                backIn.pop();
                }
                
                assert(!line.empty() && "line must not be empty!");  
                rider onRide = line.front();
    
                //takes 1 ticket way from the customer that is going on the line
                onRide.tickets--;
    
                //increments clock 
                clock++;
                if(onRide.tickets == 0)
                {
                    //write the customers lastName and clock time to the outfile
                    outFile << onRide.lastName << clock << endl;
    
                    line.pop();
                    assert(!line.empty() && "line must not be empty!");
                    onRide = line.front();
                }
                else
                {
                    backIn.push(onRide);
                    
                    line.pop();
                    assert(!line.empty() && "line must not be empty!");
                    onRide = line.front();
                }       
        outFile.close();
        }
        //signals ok to the os
        return 0;
    }
    When posting code, please think about those that you are posting the code for.
    Make sure code is properly indented.
    If you have a massive amount of comments, make a copy of your source code and remove comments that don't add anything to the understanding.
    In particular remove large blocks of commented out code. Some people are reading this is the browser, where it is impossible to see where the comments start and end.
    Try to provide something compilable. Contents of "riders.h" could be pasted directly into the source, rather than have people search for a small declaration in a lengthy thread.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21